]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/restore.c
Big backport from Enterprise
[bacula/bacula] / bacula / src / filed / restore.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2017 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *  Bacula File Daemon  restore.c Restorefiles.
21  *
22  *    Kern Sibbald, November MM
23  */
24
25 #include "bacula.h"
26 #include "filed.h"
27 #include "ch.h"
28 #include "restore.h"
29
30 #ifdef HAVE_DARWIN_OS
31 #include <sys/attr.h>
32 const bool have_darwin_os = true;
33 #else
34 const bool have_darwin_os = false;
35 #endif
36
37 #if defined(HAVE_CRYPTO)
38 const bool have_crypto = true;
39 #else
40 const bool have_crypto = false;
41 #endif
42
43 #if defined(HAVE_ACL)
44 const bool have_acl = true;
45 #else
46 const bool have_acl = false;
47 #endif
48
49 #ifdef HAVE_SHA2
50 const bool have_sha2 = true;
51 #else
52 const bool have_sha2 = false;
53 #endif
54
55 #if defined(HAVE_XATTR)
56 const bool have_xattr = true;
57 #else
58 const bool have_xattr = false;
59 #endif
60
61 /* Data received from Storage Daemon */
62 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
63
64 /* Forward referenced functions */
65 #if   defined(HAVE_LIBZ)
66 static const char *zlib_strerror(int stat);
67 const bool have_libz = true;
68 #else
69 const bool have_libz = false;
70 #endif
71 #ifdef HAVE_LZO
72 const bool have_lzo = true;
73 #else
74 const bool have_lzo = false;
75 #endif
76
77 static void deallocate_cipher(r_ctx &rctx);
78 static void deallocate_fork_cipher(r_ctx &rctx);
79 static bool verify_signature(r_ctx &rctx);
80 static void free_signature(r_ctx &rctx);
81 static void free_session(r_ctx &rctx);
82 static bool close_previous_stream(r_ctx &rctx);
83 static int32_t extract_data(r_ctx &rctx, POOLMEM *buf, int32_t buflen);
84 static bool flush_cipher(r_ctx &rctx, BFILE *bfd,  uint64_t *addr, int flags, int32_t stream,
85                   RESTORE_CIPHER_CTX *cipher_ctx);
86
87 /*
88  * Close a bfd check that we are at the expected file offset.
89  * Makes use of some code from set_attributes().
90  */
91 static int bclose_chksize(r_ctx &rctx, BFILE *bfd, boffset_t osize)
92 {
93    char ec1[50], ec2[50];
94    boffset_t fsize;
95    JCR *jcr = rctx.jcr;
96
97    fsize = blseek(bfd, 0, SEEK_CUR);
98    bclose(bfd);                              /* first close file */
99    if (fsize > 0 && fsize != osize) {
100       Qmsg3(jcr, M_WARNING, 0, _("Size of data or stream of %s not correct. Original %s, restored %s.\n"),
101             jcr->last_fname, edit_uint64(osize, ec1),
102             edit_uint64(fsize, ec2));
103       return -1;
104    }
105    return 0;
106 }
107
108 #ifdef HAVE_DARWIN_OS
109 static bool restore_finderinfo(JCR *jcr, POOLMEM *buf, int32_t buflen)
110 {
111    struct attrlist attrList;
112
113    memset(&attrList, 0, sizeof(attrList));
114    attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
115    attrList.commonattr = ATTR_CMN_FNDRINFO;
116
117    Dmsg0(130, "Restoring Finder Info\n");
118    jcr->ff->flags |= FO_HFSPLUS;
119    if (buflen != 32) {
120       Jmsg(jcr, M_WARNING, 0, _("Invalid length of Finder Info (got %d, wanted 32)\n"), buflen);
121       return false;
122    }
123
124    if (setattrlist(jcr->last_fname, &attrList, buf, buflen, 0) != 0) {
125       Jmsg(jcr, M_WARNING, 0, _("Error setting Finder Info on \"%s\"\n"), jcr->last_fname);
126       return false;
127    }
128
129    return true;
130 }
131 #else
132
133 static bool restore_finderinfo(JCR *jcr, POOLMEM *buf, int32_t buflen)
134 {
135    return true;
136 }
137
138 #endif
139
140 /*
141  * Cleanup of delayed restore stack with streams for later processing.
142  */
143 static void drop_delayed_restore_streams(r_ctx &rctx, bool reuse)
144 {
145    RESTORE_DATA_STREAM *rds;
146
147    if (!rctx.delayed_streams) {
148       if (reuse) {
149          rctx.delayed_streams = New(alist(10, owned_by_alist));
150       }
151       return;
152    }
153    if (rctx.delayed_streams->empty()) {
154       return;
155    }
156
157    foreach_alist(rds, rctx.delayed_streams) {
158       if (rds->content) {
159          free(rds->content);
160          rds->content = NULL;
161       }
162    }
163    rctx.delayed_streams->destroy();
164    if (reuse) {
165       rctx.delayed_streams->init(10, owned_by_alist);
166    }
167 }
168
169
170 /*
171  * Push a data stream onto the delayed restore stack for
172  * later processing.
173  */
174 static inline void push_delayed_restore_stream(r_ctx &rctx, char *msg, int msglen)
175 {
176    RESTORE_DATA_STREAM *rds;
177
178    if (msglen <= 0) {
179       return;
180    }
181    if (!rctx.delayed_streams) {
182       rctx.delayed_streams = New(alist(10, owned_by_alist));
183    }
184
185    rds = (RESTORE_DATA_STREAM *)malloc(sizeof(RESTORE_DATA_STREAM));
186    rds->stream = rctx.stream;
187    rds->content = (char *)malloc(msglen);
188    memcpy(rds->content, msg, msglen);
189    rds->content_length = msglen;
190    rctx.delayed_streams->append(rds);
191 }
192
193 /*
194  * Perform a restore of an ACL using the stream received.
195  * This can either be a delayed restore or direct restore.
196  */
197 static inline bool do_restore_acl(JCR *jcr, int stream, char *content,
198                                   uint32_t content_length)
199 {
200    if (!jcr->xacl) {
201       return true;
202    }
203    switch (jcr->xacl->restore_acl(jcr, stream, content, content_length)) {
204       case bRC_XACL_fatal:
205          return false;
206       case bRC_XACL_error:
207          /*
208           * Non-fatal errors, count them and when the number is under ACL_MAX_ERROR_PRINT_PER_JOB
209           * print the error message set by the lower level routine in jcr->errmsg.
210           */
211          if (jcr->xacl->get_acl_nr_errors() < ACL_MAX_ERROR_PRINT_PER_JOB) {
212             Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
213          }
214          break;
215       default:
216          break;
217    }
218    return true;
219 }
220
221 /*
222  * Perform a restore of an XATTR using the stream received.
223  * This can either be a delayed restore or direct restore.
224  */
225 static inline bool do_restore_xattr(JCR *jcr, int stream, char *content,
226                                     uint32_t content_length)
227 {
228    if (!jcr->xacl) {
229       return true;
230    }
231    switch (jcr->xacl->restore_xattr(jcr, stream, content, content_length)) {
232       case bRC_XACL_fatal:
233          return false;
234       case bRC_XACL_error:
235          /*
236           * Non-fatal errors, count them and when the number is under XATTR_MAX_ERROR_PRINT_PER_JOB
237           * print the error message set by the lower level routine in jcr->errmsg.
238           */
239          if (jcr->xacl->get_xattr_nr_errors() < XATTR_MAX_ERROR_PRINT_PER_JOB) {
240             Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
241          }
242          break;
243       default:
244          break;
245    }
246    return true;
247 }
248
249 /*
250  * Restore any data streams that are restored after the file
251  * is fully restored and has its attributes restored. Things
252  * like acls and xattr are restored after we set the file
253  * attributes otherwise we might clear some security flags
254  * by setting the attributes.
255  */
256 static inline bool pop_delayed_data_streams(r_ctx &rctx)
257 {
258    RESTORE_DATA_STREAM *rds;
259    JCR *jcr = rctx.jcr;
260
261    /*
262     * See if there is anything todo.
263     */
264    if (!rctx.delayed_streams ||
265         rctx.delayed_streams->empty()) {
266       return true;
267    }
268
269    /*
270     * Only process known delayed data streams here.
271     * If you start using more delayed data streams
272     * be sure to add them in this loop and add the
273     * proper calls here.
274     *
275     * Currently we support delayed data stream
276     * processing for the following type of streams:
277     * - *_ACL_*
278     * - *_XATTR_*
279     */
280    foreach_alist(rds, rctx.delayed_streams) {
281       switch (rds->stream) {
282       case STREAM_UNIX_ACCESS_ACL:
283       case STREAM_UNIX_DEFAULT_ACL:
284       case STREAM_XACL_AIX_TEXT:
285       case STREAM_XACL_DARWIN_ACCESS:
286       case STREAM_XACL_FREEBSD_DEFAULT:
287       case STREAM_XACL_FREEBSD_ACCESS:
288       case STREAM_XACL_HPUX_ACL_ENTRY:
289       case STREAM_XACL_IRIX_DEFAULT:
290       case STREAM_XACL_IRIX_ACCESS:
291       case STREAM_XACL_LINUX_DEFAULT:
292       case STREAM_XACL_LINUX_ACCESS:
293       case STREAM_XACL_TRU64_DEFAULT:
294       case STREAM_XACL_TRU64_DEFAULT_DIR:
295       case STREAM_XACL_TRU64_ACCESS:
296       case STREAM_XACL_SOLARIS_POSIX:
297       case STREAM_XACL_SOLARIS_NFS4:
298       case STREAM_XACL_AFS_TEXT:
299       case STREAM_XACL_AIX_AIXC:
300       case STREAM_XACL_AIX_NFS4:
301       case STREAM_XACL_FREEBSD_NFS4:
302       case STREAM_XACL_HURD_DEFAULT:
303       case STREAM_XACL_HURD_ACCESS:
304          if (!do_restore_acl(jcr, rds->stream, rds->content, rds->content_length)) {
305             goto get_out;
306          }
307          break;
308       case STREAM_XACL_HURD_XATTR:
309       case STREAM_XACL_IRIX_XATTR:
310       case STREAM_XACL_TRU64_XATTR:
311       case STREAM_XACL_AIX_XATTR:
312       case STREAM_XACL_OPENBSD_XATTR:
313       case STREAM_XACL_SOLARIS_SYS_XATTR:
314       case STREAM_XACL_DARWIN_XATTR:
315       case STREAM_XACL_FREEBSD_XATTR:
316       case STREAM_XACL_LINUX_XATTR:
317       case STREAM_XACL_NETBSD_XATTR:
318          if (!do_restore_xattr(jcr, rds->stream, rds->content, rds->content_length)) {
319             goto get_out;
320          }
321          break;
322       default:
323          Jmsg(jcr, M_WARNING, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"),
324               rds->stream);
325          break;
326       }
327       if (rds->content) {
328          free(rds->content);
329          rds->content = NULL;
330       }
331    }
332
333    drop_delayed_restore_streams(rctx, true);
334    return true;
335
336 get_out:
337    drop_delayed_restore_streams(rctx, true);
338    return false;
339 }
340
341
342 /*
343  * Restore the requested files.
344  */
345 void do_restore(JCR *jcr)
346 {
347    BSOCK *sd;
348    uint32_t VolSessionId, VolSessionTime;
349    int32_t file_index;
350    char ec1[50];                       /* Buffer printing huge values */
351    uint32_t buf_size;                  /* client buffer size */
352    int stat;
353    int64_t rsrc_len = 0;               /* Original length of resource fork */
354    r_ctx rctx;
355    ATTR *attr;
356    int bget_ret = 0;
357    /* ***FIXME*** make configurable */
358    crypto_digest_t signing_algorithm = have_sha2 ?
359                                        CRYPTO_DIGEST_SHA256 : CRYPTO_DIGEST_SHA1;
360    memset(&rctx, 0, sizeof(rctx));
361    rctx.jcr = jcr;
362
363    /* The following variables keep track of "known unknowns" */
364    int non_suppored_data = 0;
365    int non_suppored_attr = 0;
366    int non_suppored_rsrc = 0;
367    int non_suppored_finfo = 0;
368    int non_suppored_acl = 0;
369    int non_suppored_progname = 0;
370    int non_suppored_crypto = 0;
371    int non_suppored_xattr = 0;
372
373    sd = jcr->store_bsock;
374    jcr->setJobStatus(JS_Running);
375
376    LockRes();
377    CLIENT *client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
378    UnlockRes();
379    if (client) {
380       buf_size = client->max_network_buffer_size;
381    } else {
382       buf_size = 0;                   /* use default */
383    }
384    if (!sd->set_buffer_size(buf_size, BNET_SETBUF_WRITE)) {
385       jcr->setJobStatus(JS_ErrorTerminated);
386       return;
387    }
388    jcr->buf_size = sd->msglen;
389
390    /* use the same buffer size to decompress both gzip and lzo */
391    if (have_libz || have_lzo) {
392       uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
393       jcr->compress_buf = get_memory(compress_buf_size);
394       jcr->compress_buf_size = compress_buf_size;
395    }
396
397    GetMsg *fdmsg;
398    fdmsg = New(GetMsg(jcr, sd, rec_header, GETMSG_MAX_MSG_SIZE));
399
400    fdmsg->start_read_sock();
401    bmessage *bmsg = fdmsg->new_msg(); /* get a message, to exchange with fdmsg */
402
403 #ifdef HAVE_LZO
404    if (lzo_init() != LZO_E_OK) {
405       Jmsg(jcr, M_FATAL, 0, _("LZO init failed\n"));
406       goto get_out;
407    }
408 #endif
409
410    if (have_crypto) {
411       rctx.cipher_ctx.buf = get_memory(CRYPTO_CIPHER_MAX_BLOCK_SIZE);
412       if (have_darwin_os) {
413          rctx.fork_cipher_ctx.buf = get_memory(CRYPTO_CIPHER_MAX_BLOCK_SIZE);
414       }
415    }
416
417    /*
418     * Get a record from the Storage daemon. We are guaranteed to
419     *   receive records in the following order:
420     *   1. Stream record header
421     *   2. Stream data (one or more of the following in the order given)
422     *        a. Attributes (Unix or Windows)
423     *        b. Possibly stream encryption session data (e.g., symmetric session key)
424     *        c. File data for the file
425     *        d. Alternate data stream (e.g. Resource Fork)
426     *        e. Finder info
427     *        f. ACLs
428     *        g. XATTRs
429     *        h. Possibly a cryptographic signature
430     *        i. Possibly MD5 or SHA1 record
431     *   3. Repeat step 1
432     *
433     * NOTE: We keep track of two bacula file descriptors:
434     *   1. bfd for file data.
435     *      This fd is opened for non empty files when an attribute stream is
436     *      encountered and closed when we find the next attribute stream.
437     *   2. fork_bfd for alternate data streams
438     *      This fd is opened every time we encounter a new alternate data
439     *      stream for the current file. When we find any other stream, we
440     *      close it again.
441     *      The expected size of the stream, fork_len, should be set when
442     *      opening the fd.
443     *   3. Not all the stream data records are required -- e.g. if there
444     *      is no fork, there is no alternate data stream, no ACL, ...
445     */
446    binit(&rctx.bfd);
447    binit(&rctx.forkbfd);
448    attr = rctx.attr = new_attr(jcr);
449    jcr->xacl = (XACL*)new_xacl();
450
451    Dsm_check(200);
452    while ((bget_ret = fdmsg->bget_msg(&bmsg)) >= 0 && !job_canceled(jcr)) {
453       time_t now = time(NULL);
454       if (jcr->last_stat_time == 0) {
455          jcr->last_stat_time = now;
456          jcr->stat_interval = 30;  /* Default 30 seconds */
457       } else if (now >= jcr->last_stat_time + jcr->stat_interval) {
458          jcr->dir_bsock->fsend("Progress JobId=x files=%ld bytes=%lld bps=%ld\n",
459             jcr->JobFiles, jcr->JobBytes, jcr->LastRate);
460          jcr->last_stat_time = now;
461       }
462
463       /* Remember previous stream type */
464       rctx.prev_stream = rctx.stream;
465
466       /* First we expect a Stream Record Header */
467       Dsm_check(200);
468       if (sscanf(bmsg->rbuf, rec_header, &VolSessionId, &VolSessionTime, &file_index,
469           &rctx.full_stream, &rctx.size) != 5) {
470          Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), bmsg->rbuf);
471          goto get_out;
472       }
473       /* Strip off new stream high bits */
474       rctx.stream = rctx.full_stream & STREAMMASK_TYPE;
475
476       /* Now we expect the Stream Data */
477       if ((bget_ret = fdmsg->bget_msg(&bmsg)) < 0) {
478          if (bget_ret != BNET_EXT_TERMINATE) {
479             Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), sd->bstrerror());
480          } else {
481             /* The error has been handled somewhere else, just quit */
482          }
483          goto get_out;
484       }
485       if (rctx.size != (uint32_t)bmsg->origlen) {
486          Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"),
487                bmsg->origlen, rctx.size);
488          Dmsg2(50, "Actual data size %d not same as header %d\n",
489                bmsg->origlen, rctx.size);
490          goto get_out;
491       }
492
493       /* If we change streams, close and reset alternate data streams */
494       if (rctx.prev_stream != rctx.stream) {
495          if (is_bopen(&rctx.forkbfd)) {
496             deallocate_fork_cipher(rctx);
497             bclose_chksize(rctx, &rctx.forkbfd, rctx.fork_size);
498          }
499          /* Use an impossible value and set a proper one below */
500          rctx.fork_size = -1;
501          rctx.fork_addr = 0;
502       }
503
504       /* File Attributes stream */
505       switch (rctx.stream) {
506       case STREAM_UNIX_ATTRIBUTES:
507       case STREAM_UNIX_ATTRIBUTES_EX:
508          /* if any previous stream open, close it */
509          if (!close_previous_stream(rctx)) {
510             goto get_out;
511          }
512
513          /*
514           * TODO: manage deleted files
515           */
516          if (rctx.type == FT_DELETED) { /* deleted file */
517             continue;
518          }
519          /*
520           * Restore objects should be ignored here -- they are
521           * returned at the beginning of the restore.
522           */
523          if (IS_FT_OBJECT(rctx.type)) {
524             continue;
525          }
526
527          /*
528           * Unpack attributes and do sanity check them
529           */
530          if (!unpack_attributes_record(jcr, rctx.stream, bmsg->rbuf, bmsg->rbuflen, attr)) {
531             goto get_out;
532          }
533
534          attr->data_stream = decode_stat(attr->attr, &attr->statp, sizeof(attr->statp), &attr->LinkFI);
535
536          Dmsg5(100, "Stream %d: %s, File %s\nattrib=%s\nattribsEx=%s\n",
537                attr->data_stream, stream_to_ascii(attr->data_stream),
538                attr->fname, attr->attr, attr->attrEx);
539          Dmsg3(100, "=== msglen=%d attrExlen=%d msg=%s\n", bmsg->rbuflen,
540                strlen(attr->attrEx), bmsg->rbuf);
541
542          if (!is_restore_stream_supported(attr->data_stream)) {
543             Dmsg2(15, "Non-supported data stream %d: %s\n",
544                attr->data_stream, stream_to_ascii(attr->data_stream));
545             if (!non_suppored_data++) {
546                Jmsg(jcr, M_WARNING, 0, _("%s stream not supported on this Client.\n"),
547                     stream_to_ascii(attr->data_stream));
548             }
549             continue;
550          }
551
552          build_attr_output_fnames(jcr, attr);
553
554          /*
555           * Try to actually create the file, which returns a status telling
556           *  us if we need to extract or not.
557           */
558          jcr->num_files_examined++;
559          rctx.extract = false;
560          stat = CF_CORE;        /* By default, let Bacula's core handle it */
561
562          if (jcr->plugin) {
563             stat = plugin_create_file(jcr, attr, &rctx.bfd, jcr->replace);
564          }
565
566          if (stat == CF_CORE) {
567             stat = create_file(jcr, attr, &rctx.bfd, jcr->replace);
568          }
569          jcr->lock();
570          pm_strcpy(jcr->last_fname, attr->ofname);
571          jcr->last_type = attr->type;
572          jcr->unlock();
573          Dmsg2(130, "Outfile=%s create_file stat=%d\n", attr->ofname, stat);
574          switch (stat) {
575          case CF_ERROR:
576          case CF_SKIP:
577             jcr->JobFiles++;
578             break;
579          case CF_EXTRACT:      /* File created and we expect file data */
580             rctx.extract = true;
581             /* FALLTHROUGH WANTED */
582          case CF_CREATED:      /* File created, but there is no content */
583             /* File created, but there is no content */
584             rctx.fileAddr = 0;
585             print_ls_output(jcr, attr);
586
587             if (have_darwin_os) {
588                /* Only restore the resource fork for regular files */
589                from_base64(&rsrc_len, attr->attrEx);
590                if (attr->type == FT_REG && rsrc_len > 0) {
591                   rctx.extract = true;
592                }
593
594                /*
595                 * Do not count the resource forks as regular files being restored.
596                 */
597                if (rsrc_len == 0) {
598                   jcr->JobFiles++;
599                }
600             } else {
601                jcr->JobFiles++;
602             }
603
604             if (!rctx.extract) {
605                /* set attributes now because file will not be extracted */
606                if (jcr->plugin) {
607                   plugin_set_attributes(jcr, attr, &rctx.bfd);
608                } else {
609                   set_attributes(jcr, attr, &rctx.bfd);
610                }
611             }
612             break;
613          }
614
615          break;
616
617       /* Data stream */
618       case STREAM_ENCRYPTED_SESSION_DATA:
619          crypto_error_t cryptoerr;
620
621          /* The current file will not be extracted, do not create a crypto session */
622          if (!rctx.extract) {
623             break;
624          }
625
626          /* Is this an unexpected session data entry? */
627          if (rctx.cs) {
628             Jmsg0(jcr, M_ERROR, 0, _("Unexpected cryptographic session data stream.\n"));
629             rctx.extract = false;
630             bclose(&rctx.bfd);
631             continue;
632          }
633
634          /* Do we have any keys at all? */
635          if (!jcr->crypto.pki_recipients) {
636             Jmsg(jcr, M_ERROR, 0, _("No private decryption keys have been defined to decrypt encrypted backup data.\n"));
637             rctx.extract = false;
638             bclose(&rctx.bfd);
639             break;
640          }
641
642          if (jcr->crypto.digest) {
643             crypto_digest_free(jcr->crypto.digest);
644          }
645          jcr->crypto.digest = crypto_digest_new(jcr, signing_algorithm);
646          if (!jcr->crypto.digest) {
647             Jmsg0(jcr, M_FATAL, 0, _("Could not create digest.\n"));
648             rctx.extract = false;
649             bclose(&rctx.bfd);
650             break;
651          }
652
653          /* Decode and save session keys. */
654          cryptoerr = crypto_session_decode((uint8_t *)bmsg->rbuf, (uint32_t)bmsg->rbuflen,
655                         jcr->crypto.pki_recipients, &rctx.cs);
656          switch (cryptoerr) {
657          case CRYPTO_ERROR_NONE:
658             /* Success */
659             break;
660          case CRYPTO_ERROR_NORECIPIENT:
661             Jmsg(jcr, M_ERROR, 0, _("Missing private key required to decrypt encrypted backup data.\n"));
662             break;
663          case CRYPTO_ERROR_DECRYPTION:
664             Jmsg(jcr, M_ERROR, 0, _("Decrypt of the session key failed.\n"));
665             break;
666          case CRYPTO_ERROR_NOSIGNER:
667             Jmsg(jcr, M_ERROR, 0, _("Signer not found. Decryption failed.\n"));
668             break;
669          case CRYPTO_ERROR_INVALID_DIGEST:
670             Jmsg(jcr, M_ERROR, 0, _("Unsupported digest algorithm. Decrypt failed.\n"));
671             break;
672          case CRYPTO_ERROR_INVALID_CRYPTO:
673             Jmsg(jcr, M_ERROR, 0, _("Unsupported encryption algorithm. Decrypt failed.\n"));
674             break;
675          default:
676             /* This shouldn't happen */
677             Jmsg2(jcr, M_ERROR, 0, _("An error=%d occurred while decoding encrypted session data stream: ERR=%s\n"),
678                cryptoerr, crypto_strerror(cryptoerr));
679             break;
680          }
681
682          if (cryptoerr != CRYPTO_ERROR_NONE) {
683             rctx.extract = false;
684             bclose(&rctx.bfd);
685             continue;
686          }
687
688          break;
689
690       case STREAM_FILE_DATA:
691       case STREAM_SPARSE_DATA:
692       case STREAM_WIN32_DATA:
693       case STREAM_GZIP_DATA:
694       case STREAM_SPARSE_GZIP_DATA:
695       case STREAM_WIN32_GZIP_DATA:
696       case STREAM_COMPRESSED_DATA:
697       case STREAM_SPARSE_COMPRESSED_DATA:
698       case STREAM_WIN32_COMPRESSED_DATA:
699       case STREAM_ENCRYPTED_FILE_DATA:
700       case STREAM_ENCRYPTED_WIN32_DATA:
701       case STREAM_ENCRYPTED_FILE_GZIP_DATA:
702       case STREAM_ENCRYPTED_WIN32_GZIP_DATA:
703       case STREAM_ENCRYPTED_FILE_COMPRESSED_DATA:
704       case STREAM_ENCRYPTED_WIN32_COMPRESSED_DATA:
705          /* Force an expected, consistent stream type here */
706          if (rctx.extract && (rctx.prev_stream == rctx.stream
707                          || rctx.prev_stream == STREAM_UNIX_ATTRIBUTES
708                          || rctx.prev_stream == STREAM_UNIX_ATTRIBUTES_EX
709                          || rctx.prev_stream == STREAM_ENCRYPTED_SESSION_DATA)) {
710             rctx.flags = 0;
711
712             if (rctx.stream == STREAM_SPARSE_DATA
713                   || rctx.stream == STREAM_SPARSE_COMPRESSED_DATA
714                   || rctx.stream == STREAM_SPARSE_GZIP_DATA)
715             {
716                rctx.flags |= FO_SPARSE;
717             }
718
719             if (rctx.stream == STREAM_GZIP_DATA
720                   || rctx.stream == STREAM_SPARSE_GZIP_DATA
721                   || rctx.stream == STREAM_WIN32_GZIP_DATA
722                   || rctx.stream == STREAM_ENCRYPTED_FILE_GZIP_DATA
723                   || rctx.stream == STREAM_COMPRESSED_DATA
724                   || rctx.stream == STREAM_SPARSE_COMPRESSED_DATA
725                   || rctx.stream == STREAM_WIN32_COMPRESSED_DATA
726                   || rctx.stream == STREAM_ENCRYPTED_FILE_COMPRESSED_DATA
727                   || rctx.stream == STREAM_ENCRYPTED_WIN32_COMPRESSED_DATA
728                   || rctx.stream == STREAM_ENCRYPTED_WIN32_GZIP_DATA) {
729                rctx.flags |= FO_COMPRESS;
730                rctx.comp_stream = rctx.stream;
731             }
732
733             if (rctx.stream == STREAM_ENCRYPTED_FILE_DATA
734                   || rctx.stream == STREAM_ENCRYPTED_FILE_GZIP_DATA
735                   || rctx.stream == STREAM_ENCRYPTED_WIN32_DATA
736                   || rctx.stream == STREAM_ENCRYPTED_FILE_COMPRESSED_DATA
737                   || rctx.stream == STREAM_ENCRYPTED_WIN32_COMPRESSED_DATA
738                   || rctx.stream == STREAM_ENCRYPTED_WIN32_GZIP_DATA) {
739                /* Set up a decryption context */
740                if (!rctx.cipher_ctx.cipher) {
741                   if (!rctx.cs) {
742                      Jmsg1(jcr, M_ERROR, 0, _("Missing encryption session data stream for %s\n"), jcr->last_fname);
743                      rctx.extract = false;
744                      bclose(&rctx.bfd);
745                      continue;
746                   }
747
748                   if ((rctx.cipher_ctx.cipher = crypto_cipher_new(rctx.cs, false,
749                            &rctx.cipher_ctx.block_size)) == NULL) {
750                      Jmsg1(jcr, M_ERROR, 0, _("Failed to initialize decryption context for %s\n"), jcr->last_fname);
751                      free_session(rctx);
752                      rctx.extract = false;
753                      bclose(&rctx.bfd);
754                      continue;
755                   }
756                }
757                rctx.flags |= FO_ENCRYPT;
758             }
759
760             if (is_win32_stream(rctx.stream) &&
761                 (win32decomp || !have_win32_api())) {
762                set_portable_backup(&rctx.bfd);
763                rctx.flags |= FO_WIN32DECOMP; /* "decompose" BackupWrite data */
764             }
765
766             if (extract_data(rctx, bmsg->rbuf, bmsg->rbuflen) < 0) {
767                rctx.extract = false;
768                bclose(&rctx.bfd);
769                continue;
770             }
771          }
772          break;
773
774       /*
775        * Resource fork stream - only recorded after a file to be restored
776        * Silently ignore if we cannot write - we already reported that
777        */
778       case STREAM_ENCRYPTED_MACOS_FORK_DATA:
779       case STREAM_MACOS_FORK_DATA:
780          if (have_darwin_os) {
781             rctx.fork_flags = 0;
782             jcr->ff->flags |= FO_HFSPLUS;
783
784             if (rctx.stream == STREAM_ENCRYPTED_MACOS_FORK_DATA) {
785                rctx.fork_flags |= FO_ENCRYPT;
786
787                /* Set up a decryption context */
788                if (rctx.extract && !rctx.fork_cipher_ctx.cipher) {
789                   if (!rctx.cs) {
790                      Jmsg1(jcr, M_ERROR, 0, _("Missing encryption session data stream for %s\n"), jcr->last_fname);
791                      rctx.extract = false;
792                      bclose(&rctx.bfd);
793                      continue;
794                   }
795
796                   if ((rctx.fork_cipher_ctx.cipher = crypto_cipher_new(rctx.cs, false, &rctx.fork_cipher_ctx.block_size)) == NULL) {
797                      Jmsg1(jcr, M_ERROR, 0, _("Failed to initialize decryption context for %s\n"), jcr->last_fname);
798                      free_session(rctx);
799                      rctx.extract = false;
800                      bclose(&rctx.bfd);
801                      continue;
802                   }
803                }
804             }
805
806             if (rctx.extract) {
807                if (rctx.prev_stream != rctx.stream) {
808                   if (bopen_rsrc(&rctx.forkbfd, jcr->last_fname, O_WRONLY | O_TRUNC | O_BINARY, 0) < 0) {
809                      Jmsg(jcr, M_WARNING, 0, _("Cannot open resource fork for %s.\n"), jcr->last_fname);
810                      rctx.extract = false;
811                      continue;
812                   }
813
814                   rctx.fork_size = rsrc_len;
815                   Dmsg0(130, "Restoring resource fork\n");
816                }
817
818                if (extract_data(rctx, bmsg->rbuf, bmsg->rbuflen) < 0) {
819                   rctx.extract = false;
820                   bclose(&rctx.forkbfd);
821                   continue;
822                }
823             }
824          } else {
825             non_suppored_rsrc++;
826          }
827          break;
828
829       case STREAM_HFSPLUS_ATTRIBUTES:
830          if (have_darwin_os) {
831             if (!restore_finderinfo(jcr, bmsg->rbuf, bmsg->rbuflen)) {
832                continue;
833             }
834          } else {
835             non_suppored_finfo++;
836          }
837          break;
838
839       case STREAM_UNIX_ACCESS_ACL:
840       case STREAM_UNIX_DEFAULT_ACL:
841       case STREAM_XACL_AIX_TEXT:
842       case STREAM_XACL_DARWIN_ACCESS:
843       case STREAM_XACL_FREEBSD_DEFAULT:
844       case STREAM_XACL_FREEBSD_ACCESS:
845       case STREAM_XACL_HPUX_ACL_ENTRY:
846       case STREAM_XACL_IRIX_DEFAULT:
847       case STREAM_XACL_IRIX_ACCESS:
848       case STREAM_XACL_LINUX_DEFAULT:
849       case STREAM_XACL_LINUX_ACCESS:
850       case STREAM_XACL_TRU64_DEFAULT:
851       case STREAM_XACL_TRU64_DEFAULT_DIR:
852       case STREAM_XACL_TRU64_ACCESS:
853       case STREAM_XACL_SOLARIS_POSIX:
854       case STREAM_XACL_SOLARIS_NFS4:
855       case STREAM_XACL_AFS_TEXT:
856       case STREAM_XACL_AIX_AIXC:
857       case STREAM_XACL_AIX_NFS4:
858       case STREAM_XACL_FREEBSD_NFS4:
859       case STREAM_XACL_HURD_DEFAULT:
860       case STREAM_XACL_HURD_ACCESS:
861          /*
862           * Do not restore ACLs when
863           * a) The current file is not extracted
864           * b)     and it is not a directory (they are never "extracted")
865           * c) or the file name is empty
866           */
867          if ((!rctx.extract &&
868                jcr->last_type != FT_DIREND) ||
869              (*jcr->last_fname == 0)) {
870             break;
871          }
872          if (have_acl) {
873             /*
874              * For anything that is not a directory we delay
875              * the restore of acls till a later stage.
876              */
877             if (jcr->last_type != FT_DIREND) {
878                push_delayed_restore_stream(rctx, bmsg->rbuf, bmsg->rbuflen);
879             } else {
880                if (!do_restore_acl(jcr, rctx.stream, bmsg->rbuf, bmsg->rbuflen)) {
881                   goto get_out;
882                }
883             }
884          } else {
885             non_suppored_acl++;
886          }
887          break;
888
889       case STREAM_XACL_HURD_XATTR:
890       case STREAM_XACL_IRIX_XATTR:
891       case STREAM_XACL_TRU64_XATTR:
892       case STREAM_XACL_AIX_XATTR:
893       case STREAM_XACL_OPENBSD_XATTR:
894       case STREAM_XACL_SOLARIS_SYS_XATTR:
895       case STREAM_XACL_DARWIN_XATTR:
896       case STREAM_XACL_FREEBSD_XATTR:
897       case STREAM_XACL_LINUX_XATTR:
898       case STREAM_XACL_NETBSD_XATTR:
899          /*
900           * Do not restore Extended Attributes when
901           * a) The current file is not extracted
902           * b)     and it is not a directory (they are never "extracted")
903           * c) or the file name is empty
904           */
905          if ((!rctx.extract &&
906                jcr->last_type != FT_DIREND) ||
907              (*jcr->last_fname == 0)) {
908             break;
909          }
910          if (have_xattr) {
911             /*
912              * For anything that is not a directory we delay
913              * the restore of xattr till a later stage.
914              */
915             if (jcr->last_type != FT_DIREND) {
916                push_delayed_restore_stream(rctx, bmsg->rbuf, bmsg->rbuflen);
917             } else {
918                if (!do_restore_xattr(jcr, rctx.stream, bmsg->rbuf, bmsg->rbuflen)) {
919                   goto get_out;
920                }
921             }
922          } else {
923             non_suppored_xattr++;
924          }
925          break;
926
927       case STREAM_XACL_SOLARIS_XATTR:
928          /*
929           * Do not restore Extended Attributes when
930           * a) The current file is not extracted
931           * b)     and it is not a directory (they are never "extracted")
932           * c) or the file name is empty
933           */
934          if ((!rctx.extract &&
935                jcr->last_type != FT_DIREND) ||
936              (*jcr->last_fname == 0)) {
937             break;
938          }
939          if (have_xattr) {
940             if (!do_restore_xattr(jcr, rctx.stream, bmsg->rbuf, bmsg->rbuflen)) {
941                goto get_out;
942             }
943          } else {
944             non_suppored_xattr++;
945          }
946          break;
947
948       case STREAM_SIGNED_DIGEST:
949          /* Is this an unexpected signature? */
950          if (rctx.sig) {
951             Jmsg0(jcr, M_ERROR, 0, _("Unexpected cryptographic signature data stream.\n"));
952             free_signature(rctx);
953             continue;
954          }
955          /* Save signature. */
956          if (rctx.extract && (rctx.sig = crypto_sign_decode(jcr, (uint8_t *)bmsg->rbuf, (uint32_t)bmsg->rbuflen)) == NULL) {
957             Jmsg1(jcr, M_ERROR, 0, _("Failed to decode message signature for %s\n"), jcr->last_fname);
958          }
959          break;
960
961       case STREAM_MD5_DIGEST:
962       case STREAM_SHA1_DIGEST:
963       case STREAM_SHA256_DIGEST:
964       case STREAM_SHA512_DIGEST:
965          break;
966
967       case STREAM_PROGRAM_NAMES:
968       case STREAM_PROGRAM_DATA:
969          if (!non_suppored_progname) {
970             Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
971             non_suppored_progname++;
972          }
973          break;
974
975       case STREAM_PLUGIN_NAME:
976          if (!close_previous_stream(rctx)) {
977             goto get_out;
978          }
979          Dmsg1(150, "restore stream_plugin_name=%s\n", bmsg->rbuf);
980          plugin_name_stream(jcr, bmsg->rbuf);
981          break;
982
983       case STREAM_RESTORE_OBJECT:
984          break;                    /* these are sent by Director */
985
986       default:
987          if (!close_previous_stream(rctx)) {
988             goto get_out;
989          }
990          Jmsg(jcr, M_WARNING, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"),
991               rctx.stream);
992          Dmsg2(0, "Unknown stream=%d data=%s\n", rctx.stream, bmsg->rbuf);
993          break;
994       } /* end switch(stream) */
995
996       /* Debug code: check if we must hangup or blowup */
997       if (handle_hangup_blowup(jcr, jcr->JobFiles, jcr->JobBytes)) {
998          goto get_out;
999       }
1000
1001       Dsm_check(200);
1002    } /* end while bufmsg->bget_msg(&bmsg)) */
1003
1004    if (bget_ret == BNET_EXT_TERMINATE) {
1005       goto get_out;
1006    }
1007    /*
1008     * If output file is still open, it was the last one in the
1009     * archive since we just hit an end of file, so close the file.
1010     */
1011    if (is_bopen(&rctx.forkbfd)) {
1012       bclose_chksize(rctx, &rctx.forkbfd, rctx.fork_size);
1013    }
1014
1015    if (!close_previous_stream(rctx)) {
1016       goto get_out;
1017    }
1018    jcr->setJobStatus(JS_Terminated);
1019    goto ok_out;
1020
1021 get_out:
1022    jcr->setJobStatus(JS_ErrorTerminated);
1023
1024 ok_out:
1025    Dsm_check(200);
1026    fdmsg->wait_read_sock(jcr->is_job_canceled());
1027    delete bmsg;
1028    free_GetMsg(fdmsg);
1029    Dsm_check(200);
1030    /*
1031     * First output the statistics.
1032     */
1033    Dmsg2(10, "End Do Restore. Files=%d Bytes=%s\n", jcr->JobFiles,
1034       edit_uint64(jcr->JobBytes, ec1));
1035
1036    if (jcr->xacl) {
1037       if (jcr->xacl->get_acl_nr_errors() > 0) {
1038          Jmsg(jcr, M_WARNING, 0, _("Encountered %ld acl errors while doing restore\n"), jcr->xacl->get_acl_nr_errors());
1039       }
1040       if (jcr->xacl->get_xattr_nr_errors() > 0) {
1041          Jmsg(jcr, M_WARNING, 0, _("Encountered %ld xattr errors while doing restore\n"), jcr->xacl->get_xattr_nr_errors());
1042       }
1043    }
1044    if (non_suppored_data > 1 || non_suppored_attr > 1) {
1045       Jmsg(jcr, M_WARNING, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
1046          non_suppored_data, non_suppored_attr);
1047    }
1048    if (non_suppored_rsrc) {
1049       Jmsg(jcr, M_INFO, 0, _("%d non-supported resource fork streams ignored.\n"), non_suppored_rsrc);
1050    }
1051    if (non_suppored_finfo) {
1052       Jmsg(jcr, M_INFO, 0, _("%d non-supported Finder Info streams ignored.\n"), non_suppored_finfo);
1053    }
1054    if (non_suppored_acl) {
1055       Jmsg(jcr, M_INFO, 0, _("%d non-supported acl streams ignored.\n"), non_suppored_acl);
1056    }
1057    if (non_suppored_crypto) {
1058       Jmsg(jcr, M_INFO, 0, _("%d non-supported crypto streams ignored.\n"), non_suppored_acl);
1059    }
1060    if (non_suppored_xattr) {
1061       Jmsg(jcr, M_INFO, 0, _("%d non-supported xattr streams ignored.\n"), non_suppored_xattr);
1062    }
1063
1064    /* Free Signature & Crypto Data */
1065    free_signature(rctx);
1066    free_session(rctx);
1067    if (jcr->crypto.digest) {
1068       crypto_digest_free(jcr->crypto.digest);
1069       jcr->crypto.digest = NULL;
1070    }
1071
1072    /* Free file cipher restore context */
1073    if (rctx.cipher_ctx.cipher) {
1074       crypto_cipher_free(rctx.cipher_ctx.cipher);
1075       rctx.cipher_ctx.cipher = NULL;
1076    }
1077
1078    if (rctx.cipher_ctx.buf) {
1079       free_pool_memory(rctx.cipher_ctx.buf);
1080       rctx.cipher_ctx.buf = NULL;
1081    }
1082
1083    /* Free alternate stream cipher restore context */
1084    if (rctx.fork_cipher_ctx.cipher) {
1085       crypto_cipher_free(rctx.fork_cipher_ctx.cipher);
1086       rctx.fork_cipher_ctx.cipher = NULL;
1087    }
1088    if (rctx.fork_cipher_ctx.buf) {
1089       free_pool_memory(rctx.fork_cipher_ctx.buf);
1090       rctx.fork_cipher_ctx.buf = NULL;
1091    }
1092
1093    if (jcr->compress_buf) {
1094       free_pool_memory(jcr->compress_buf);
1095       jcr->compress_buf = NULL;
1096       jcr->compress_buf_size = 0;
1097    }
1098
1099    if (jcr->xacl) {
1100       delete(jcr->xacl);
1101       jcr->xacl = NULL;
1102    }
1103
1104    /* Free the delayed stream stack list. */
1105    if (rctx.delayed_streams) {
1106       drop_delayed_restore_streams(rctx, false);
1107       delete rctx.delayed_streams;
1108    }
1109
1110    Dsm_check(200);
1111    bclose(&rctx.forkbfd);
1112    bclose(&rctx.bfd);
1113    free_attr(rctx.attr);
1114 }
1115
1116 #ifdef HAVE_LIBZ
1117 /*
1118  * Convert ZLIB error code into an ASCII message
1119  */
1120 static const char *zlib_strerror(int stat)
1121 {
1122    if (stat >= 0) {
1123       return _("None");
1124    }
1125    switch (stat) {
1126    case Z_ERRNO:
1127       return _("Zlib errno");
1128    case Z_STREAM_ERROR:
1129       return _("Zlib stream error");
1130    case Z_DATA_ERROR:
1131       return _("Zlib data error");
1132    case Z_MEM_ERROR:
1133       return _("Zlib memory error");
1134    case Z_BUF_ERROR:
1135       return _("Zlib buffer error");
1136    case Z_VERSION_ERROR:
1137       return _("Zlib version error");
1138    default:
1139       return _("*none*");
1140    }
1141 }
1142 #endif
1143
1144 static int do_file_digest(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
1145 {
1146    Dmsg1(50, "do_file_digest jcr=%p\n", jcr);
1147    return (digest_file(jcr, ff_pkt, jcr->crypto.digest));
1148 }
1149
1150 bool sparse_data(JCR *jcr, BFILE *bfd, uint64_t *addr, char **data, uint32_t *length, int flags)
1151 {
1152    unser_declare;
1153    uint64_t faddr;
1154    char ec1[50];
1155    unser_begin(*data, OFFSET_FADDR_SIZE);
1156    unser_uint64(faddr);
1157    /* We seek only if we have a SPARSE stream, not for OFFSET */
1158    if ((flags & FO_SPARSE) && *addr != faddr) {
1159       *addr = faddr;
1160       if (blseek(bfd, (boffset_t)*addr, SEEK_SET) < 0) {
1161          berrno be;
1162          Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
1163                edit_uint64(*addr, ec1), jcr->last_fname,
1164                be.bstrerror(bfd->berrno));
1165          return false;
1166       }
1167    }
1168    *data += OFFSET_FADDR_SIZE;
1169    *length -= OFFSET_FADDR_SIZE;
1170    return true;
1171 }
1172
1173 bool decompress_data(JCR *jcr, int32_t stream, char **data, uint32_t *length)
1174 {
1175 #if defined(HAVE_LZO) || defined(HAVE_LIBZ)
1176    char ec1[50];                   /* Buffer printing huge values */
1177 #endif
1178
1179    Dmsg1(200, "Stream found in decompress_data(): %d\n", stream);
1180    if(stream == STREAM_COMPRESSED_DATA || stream == STREAM_SPARSE_COMPRESSED_DATA || stream == STREAM_WIN32_COMPRESSED_DATA
1181        || stream == STREAM_ENCRYPTED_FILE_COMPRESSED_DATA || stream == STREAM_ENCRYPTED_WIN32_COMPRESSED_DATA)
1182    {
1183       uint32_t comp_magic, comp_len;
1184       uint16_t comp_level, comp_version;
1185 #ifdef HAVE_LZO
1186       lzo_uint compress_len;
1187       const unsigned char *cbuf;
1188       int r, real_compress_len;
1189 #endif
1190
1191       /* read compress header */
1192       unser_declare;
1193       unser_begin(*data, sizeof(comp_stream_header));
1194       unser_uint32(comp_magic);
1195       unser_uint32(comp_len);
1196       unser_uint16(comp_level);
1197       unser_uint16(comp_version);
1198       Dmsg4(200, "Compressed data stream found: magic=0x%x, len=%d, level=%d, ver=0x%x\n", comp_magic, comp_len,
1199                               comp_level, comp_version);
1200
1201       /* version check */
1202       if (comp_version != COMP_HEAD_VERSION) {
1203          Qmsg(jcr, M_ERROR, 0, _("Compressed header version error. Got=0x%x want=0x%x\n"), comp_version, COMP_HEAD_VERSION);
1204          return false;
1205       }
1206       /* size check */
1207       if (comp_len + sizeof(comp_stream_header) != *length) {
1208          Qmsg(jcr, M_ERROR, 0, _("Compressed header size error. comp_len=%d, msglen=%d\n"),
1209               comp_len, *length);
1210          return false;
1211       }
1212       switch(comp_magic) {
1213 #ifdef HAVE_LZO
1214          case COMPRESS_LZO1X:
1215             compress_len = jcr->compress_buf_size;
1216             cbuf = (const unsigned char*)*data + sizeof(comp_stream_header);
1217             real_compress_len = *length - sizeof(comp_stream_header);
1218             Dmsg2(200, "Comp_len=%d msglen=%d\n", compress_len, *length);
1219             while ((r=lzo1x_decompress_safe(cbuf, real_compress_len,
1220                                             (unsigned char *)jcr->compress_buf, &compress_len, NULL)) == LZO_E_OUTPUT_OVERRUN)
1221             {
1222                /*
1223                 * The buffer size is too small, try with a bigger one
1224                 */
1225                compress_len = jcr->compress_buf_size = jcr->compress_buf_size + (jcr->compress_buf_size >> 1);
1226                Dmsg2(200, "Comp_len=%d msglen=%d\n", compress_len, *length);
1227                jcr->compress_buf = check_pool_memory_size(jcr->compress_buf,
1228                                                     compress_len);
1229             }
1230             if (r != LZO_E_OK) {
1231                Qmsg(jcr, M_ERROR, 0, _("LZO uncompression error on file %s. ERR=%d\n"),
1232                     jcr->last_fname, r);
1233                return false;
1234             }
1235             *data = jcr->compress_buf;
1236             *length = compress_len;
1237             Dmsg2(200, "Write uncompressed %d bytes, total before write=%s\n", compress_len, edit_uint64(jcr->JobBytes, ec1));
1238             return true;
1239 #endif
1240          default:
1241             Qmsg(jcr, M_ERROR, 0, _("Compression algorithm 0x%x found, but not supported!\n"), comp_magic);
1242             return false;
1243       }
1244     } else {
1245 #ifdef HAVE_LIBZ
1246       uLong compress_len;
1247       int stat;
1248
1249       /*
1250        * NOTE! We only use uLong and Byte because they are
1251        *  needed by the zlib routines, they should not otherwise
1252        *  be used in Bacula.
1253        */
1254       compress_len = jcr->compress_buf_size;
1255       Dmsg2(200, "Comp_len=%d msglen=%d\n", compress_len, *length);
1256       while ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
1257                               (const Byte *)*data, (uLong)*length)) == Z_BUF_ERROR)
1258       {
1259          /* The buffer size is too small, try with a bigger one. */
1260          compress_len = jcr->compress_buf_size = jcr->compress_buf_size + (jcr->compress_buf_size >> 1);
1261          Dmsg2(200, "Comp_len=%d msglen=%d\n", compress_len, *length);
1262          jcr->compress_buf = check_pool_memory_size(jcr->compress_buf,
1263                                                     compress_len);
1264       }
1265       if (stat != Z_OK) {
1266          Qmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"),
1267               jcr->last_fname, zlib_strerror(stat));
1268          return false;
1269       }
1270       *data = jcr->compress_buf;
1271       *length = compress_len;
1272       Dmsg2(200, "Write uncompressed %d bytes, total before write=%s\n", compress_len, edit_uint64(jcr->JobBytes, ec1));
1273       return true;
1274 #else
1275       Qmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
1276       return false;
1277 #endif
1278    }
1279 }
1280
1281 static void unser_crypto_packet_len(RESTORE_CIPHER_CTX *ctx)
1282 {
1283    unser_declare;
1284    if (ctx->packet_len == 0 && ctx->buf_len >= CRYPTO_LEN_SIZE) {
1285       unser_begin(&ctx->buf[0], CRYPTO_LEN_SIZE);
1286       unser_uint32(ctx->packet_len);
1287       ctx->packet_len += CRYPTO_LEN_SIZE;
1288    }
1289 }
1290
1291 static bool store_data(r_ctx &rctx, char *data, const int32_t length, bool win32_decomp)
1292 {
1293    JCR *jcr = rctx.jcr;
1294    BFILE *bfd = &rctx.bfd;
1295    ssize_t wstat;
1296
1297    if (jcr->crypto.digest) {
1298       crypto_digest_update(jcr->crypto.digest, (uint8_t *)data, length);
1299    }
1300    if (win32_decomp) {
1301       if (!processWin32BackupAPIBlock(bfd, data, length)) {
1302          berrno be;
1303          Jmsg2(jcr, M_ERROR, 0, _("Write error in Win32 Block Decomposition on %s: %s\n"),
1304                jcr->last_fname, be.bstrerror(bfd->berrno));
1305          return false;
1306       }
1307    } else if ((wstat=bwrite(bfd, data, length)) != (ssize_t)length) {
1308       berrno be;
1309       int type = M_ERROR;
1310       int len = strlen(jcr->last_fname);
1311       /*
1312        * If this is the first write and the "file" is a directory
1313        *  or a drive letter, then only issue a warning as we are
1314        *  not able to reset the metadata, then continue.
1315        * If the above is true and we have an error code 91
1316        *  (directory not empty), supress the error entirely.
1317        */
1318       if (bfd->block == 0 && len >= 2 && (jcr->last_fname[len-1] == '/' ||
1319           jcr->last_fname[len-1] == ':')) {
1320          type = M_WARNING;
1321          if (bfd->lerror == 91) {  /* Directory not empty */
1322             type = 0;              /* suppress error */
1323          }
1324       }
1325       if (type != 0) {
1326          if (wstat >= 0) {
1327             /* Insufficient bytes written */
1328             Jmsg4(jcr, type, 0, _("Wrong write size error at byte=%lld block=%d wanted=%d wrote=%d\n"),
1329                bfd->total_bytes, bfd->block, length, wstat);
1330          } else {
1331             /* Error */
1332             Jmsg6(jcr, type, 0, _("Write error at byte=%lld block=%d write_len=%d lerror=%d on %s: ERR=%s\n"),
1333                bfd->total_bytes, bfd->block, length, bfd->lerror,
1334                jcr->last_fname, be.bstrerror(bfd->berrno));
1335          }
1336       }
1337
1338       /* Ignore errors? */
1339       if (type == M_WARNING || type == 0 || no_win32_write_errors) {
1340          return true;
1341       }
1342       return false;
1343    }
1344    return true;
1345 }
1346
1347 /*
1348  * In the context of jcr, write data to bfd.
1349  * We write buflen bytes in buf at addr. addr is updated in place.
1350  * The flags specify whether to use sparse files or compression.
1351  * Return value is the number of bytes written, or -1 on errors.
1352  */
1353 int32_t extract_data(r_ctx &rctx, POOLMEM *buf, int32_t buflen)
1354 {
1355    JCR *jcr = rctx.jcr;
1356    BFILE *bfd = &rctx.bfd;
1357    int flags = rctx.flags;
1358    int32_t stream = rctx.stream;
1359    RESTORE_CIPHER_CTX *cipher_ctx = &rctx.cipher_ctx;
1360    char *wbuf;                        /* write buffer */
1361    uint32_t wsize;                    /* write size */
1362    uint32_t rsize;                    /* read size */
1363    uint32_t decrypted_len = 0;        /* Decryption output length */
1364    char ec1[50];                      /* Buffer printing huge values */
1365
1366    rsize = buflen;
1367    jcr->ReadBytes += rsize;
1368    wsize = rsize;
1369    wbuf = buf;
1370
1371    if (flags & FO_ENCRYPT) {
1372       ASSERT(cipher_ctx->cipher);
1373
1374       /*
1375        * Grow the crypto buffer, if necessary.
1376        * crypto_cipher_update() will process only whole blocks,
1377        * buffering the remaining input.
1378        */
1379       cipher_ctx->buf = check_pool_memory_size(cipher_ctx->buf,
1380                         cipher_ctx->buf_len + wsize + cipher_ctx->block_size);
1381
1382       /* Decrypt the input block */
1383       if (!crypto_cipher_update(cipher_ctx->cipher,
1384                                 (const u_int8_t *)wbuf,
1385                                 wsize,
1386                                 (u_int8_t *)&cipher_ctx->buf[cipher_ctx->buf_len],
1387                                 &decrypted_len)) {
1388          /* Decryption failed. Shouldn't happen. */
1389          Jmsg(jcr, M_FATAL, 0, _("Decryption error\n"));
1390          goto get_out;
1391       }
1392
1393       if (decrypted_len == 0) {
1394          /* No full block of encrypted data available, write more data */
1395          return 0;
1396       }
1397
1398       Dmsg2(200, "decrypted len=%d encrypted len=%d\n", decrypted_len, wsize);
1399
1400       cipher_ctx->buf_len += decrypted_len;
1401       wbuf = cipher_ctx->buf;
1402
1403       /* If one full preserved block is available, write it to disk,
1404        *  and then buffer any remaining data. This should be effecient
1405        *  as long as Bacula's block size is not significantly smaller than the
1406        *  encryption block size (extremely unlikely!)
1407        */
1408       unser_crypto_packet_len(cipher_ctx);
1409       Dmsg1(500, "Crypto unser block size=%d\n", cipher_ctx->packet_len - CRYPTO_LEN_SIZE);
1410
1411       if (cipher_ctx->packet_len == 0 || cipher_ctx->buf_len < cipher_ctx->packet_len) {
1412          /* No full preserved block is available. */
1413          return 0;
1414       }
1415
1416       /* We have one full block, set up the filter input buffers */
1417       wsize = cipher_ctx->packet_len - CRYPTO_LEN_SIZE;
1418       wbuf = &wbuf[CRYPTO_LEN_SIZE]; /* Skip the block length header */
1419       cipher_ctx->buf_len -= cipher_ctx->packet_len;
1420       Dmsg2(130, "Encryption writing full block, %u bytes, remaining %u bytes in buffer\n", wsize, cipher_ctx->buf_len);
1421    }
1422
1423    if ((flags & FO_SPARSE) || (flags & FO_OFFSETS)) {
1424       if (!sparse_data(jcr, bfd, &rctx.fileAddr, &wbuf, &wsize, flags)) {
1425          goto get_out;
1426       }
1427    }
1428
1429    if (flags & FO_COMPRESS) {
1430       if (!decompress_data(jcr, stream, &wbuf, &wsize)) {
1431          goto get_out;
1432       }
1433    }
1434
1435    if (!store_data(rctx, wbuf, wsize, (flags & FO_WIN32DECOMP) != 0)) {
1436       goto get_out;
1437    }
1438    jcr->JobBytes += wsize;
1439    rctx.fileAddr += wsize;
1440    Dmsg2(130, "Write %u bytes, JobBytes=%s\n", wsize, edit_uint64(jcr->JobBytes, ec1));
1441
1442    /* Clean up crypto buffers */
1443    if (flags & FO_ENCRYPT) {
1444       /* Move any remaining data to start of buffer */
1445       if (cipher_ctx->buf_len > 0) {
1446          Dmsg1(130, "Moving %u buffered bytes to start of buffer\n", cipher_ctx->buf_len);
1447          memmove(cipher_ctx->buf, &cipher_ctx->buf[cipher_ctx->packet_len],
1448             cipher_ctx->buf_len);
1449       }
1450       /* The packet was successfully written, reset the length so that
1451        *  the next packet length may be re-read by unser_crypto_packet_len() */
1452       cipher_ctx->packet_len = 0;
1453    }
1454    return wsize;
1455
1456 get_out:
1457    return -1;
1458 }
1459
1460 /*
1461  * If extracting, close any previous stream
1462  */
1463 static bool close_previous_stream(r_ctx &rctx)
1464 {
1465    bool rtn = true;
1466
1467    /*
1468     * If extracting, it was from previous stream, so
1469     * close the output file and validate the signature.
1470     */
1471    if (rctx.extract) {
1472       if (rctx.size > 0 && !is_bopen(&rctx.bfd)) {
1473          Jmsg0(rctx.jcr, M_ERROR, 0, _("Logic error: output file should be open\n"));
1474          Pmsg2(000, "=== logic error size=%d bopen=%d\n", rctx.size,
1475             is_bopen(&rctx.bfd));
1476       }
1477
1478       if (rctx.prev_stream != STREAM_ENCRYPTED_SESSION_DATA) {
1479          deallocate_cipher(rctx);
1480          deallocate_fork_cipher(rctx);
1481       }
1482
1483       if (rctx.jcr->plugin) {
1484          plugin_set_attributes(rctx.jcr, rctx.attr, &rctx.bfd);
1485       } else {
1486          set_attributes(rctx.jcr, rctx.attr, &rctx.bfd);
1487       }
1488       rctx.extract = false;
1489
1490       /* Now perform the delayed restore of some specific data streams. */
1491       rtn = pop_delayed_data_streams(rctx);
1492
1493       /* Verify the cryptographic signature, if any */
1494       rctx.type = rctx.attr->type;
1495       verify_signature(rctx);
1496
1497       /* Free Signature */
1498       free_signature(rctx);
1499       free_session(rctx);
1500       rctx.jcr->ff->flags = 0;
1501       Dmsg0(130, "Stop extracting.\n");
1502    } else if (is_bopen(&rctx.bfd)) {
1503       Jmsg0(rctx.jcr, M_ERROR, 0, _("Logic error: output file should not be open\n"));
1504       Pmsg0(000, "=== logic error !open\n");
1505       bclose(&rctx.bfd);
1506    }
1507
1508    return rtn;
1509 }
1510
1511 /*
1512  * In the context of jcr, flush any remaining data from the cipher context,
1513  * writing it to bfd.
1514  * Return value is true on success, false on failure.
1515  */
1516 bool flush_cipher(r_ctx &rctx, BFILE *bfd, uint64_t *addr, int flags, int32_t stream,
1517                   RESTORE_CIPHER_CTX *cipher_ctx)
1518 {
1519    JCR *jcr = rctx.jcr;
1520    uint32_t decrypted_len = 0;
1521    char *wbuf;                        /* write buffer */
1522    uint32_t wsize;                    /* write size */
1523    char ec1[50];                      /* Buffer printing huge values */
1524    bool second_pass = false;
1525
1526 again:
1527    /* Write out the remaining block and free the cipher context */
1528    cipher_ctx->buf = check_pool_memory_size(cipher_ctx->buf,
1529                         cipher_ctx->buf_len + cipher_ctx->block_size);
1530
1531    if (!crypto_cipher_finalize(cipher_ctx->cipher, (uint8_t *)&cipher_ctx->buf[cipher_ctx->buf_len],
1532         &decrypted_len)) {
1533       /* Writing out the final, buffered block failed. Shouldn't happen. */
1534       Jmsg3(jcr, M_ERROR, 0, _("Decryption error. buf_len=%d decrypt_len=%d on file %s\n"),
1535             cipher_ctx->buf_len, decrypted_len, jcr->last_fname);
1536    }
1537
1538    Dmsg2(130, "Flush decrypt len=%d buf_len=%d\n", decrypted_len, cipher_ctx->buf_len);
1539    /* If nothing new was decrypted, and our output buffer is empty, return */
1540    if (decrypted_len == 0 && cipher_ctx->buf_len == 0) {
1541       return true;
1542    }
1543
1544    cipher_ctx->buf_len += decrypted_len;
1545
1546    unser_crypto_packet_len(cipher_ctx);
1547    Dmsg1(500, "Crypto unser block size=%d\n", cipher_ctx->packet_len - CRYPTO_LEN_SIZE);
1548    wsize = cipher_ctx->packet_len - CRYPTO_LEN_SIZE;
1549    /* Decrypted, possibly decompressed output here. */
1550    wbuf = &cipher_ctx->buf[CRYPTO_LEN_SIZE]; /* Skip the block length header */
1551    cipher_ctx->buf_len -= cipher_ctx->packet_len;
1552    Dmsg2(130, "Encryption writing full block, %u bytes, remaining %u bytes in buffer\n", wsize, cipher_ctx->buf_len);
1553
1554    if ((flags & FO_SPARSE) || (flags & FO_OFFSETS)) {
1555       if (!sparse_data(jcr, bfd, addr, &wbuf, &wsize, flags)) {
1556          return false;
1557       }
1558    }
1559
1560    if (flags & FO_COMPRESS) {
1561       if (!decompress_data(jcr, stream, &wbuf, &wsize)) {
1562          return false;
1563       }
1564    }
1565
1566    Dmsg0(130, "Call store_data\n");
1567    if (!store_data(rctx, wbuf, wsize, (flags & FO_WIN32DECOMP) != 0)) {
1568       return false;
1569    }
1570    jcr->JobBytes += wsize;
1571    Dmsg2(130, "Flush write %u bytes, JobBytes=%s\n", wsize, edit_uint64(jcr->JobBytes, ec1));
1572
1573    /* Move any remaining data to start of buffer. */
1574    if (cipher_ctx->buf_len > 0) {
1575       Dmsg1(130, "Moving %u buffered bytes to start of buffer\n", cipher_ctx->buf_len);
1576       memmove(cipher_ctx->buf, &cipher_ctx->buf[cipher_ctx->packet_len],
1577          cipher_ctx->buf_len);
1578    }
1579    /* The packet was successfully written, reset the length so that the next
1580     *  packet length may be re-read by unser_crypto_packet_len() */
1581    cipher_ctx->packet_len = 0;
1582
1583    if (cipher_ctx->buf_len >0 && !second_pass) {
1584       second_pass = true;
1585       goto again;
1586    }
1587
1588    /* Stop decryption */
1589    cipher_ctx->buf_len = 0;
1590    cipher_ctx->packet_len = 0;
1591
1592    return true;
1593 }
1594
1595 static void deallocate_cipher(r_ctx &rctx)
1596 {
1597     /* Flush and deallocate previous stream's cipher context */
1598    if (rctx.cipher_ctx.cipher) {
1599       flush_cipher(rctx, &rctx.bfd, &rctx.fileAddr, rctx.flags, rctx.comp_stream, &rctx.cipher_ctx);
1600       crypto_cipher_free(rctx.cipher_ctx.cipher);
1601       rctx.cipher_ctx.cipher = NULL;
1602    }
1603 }
1604
1605 static void deallocate_fork_cipher(r_ctx &rctx)
1606 {
1607
1608    /* Flush and deallocate previous stream's fork cipher context */
1609    if (rctx.fork_cipher_ctx.cipher) {
1610       flush_cipher(rctx, &rctx.forkbfd, &rctx.fork_addr, rctx.fork_flags, rctx.comp_stream, &rctx.fork_cipher_ctx);
1611       crypto_cipher_free(rctx.fork_cipher_ctx.cipher);
1612       rctx.fork_cipher_ctx.cipher = NULL;
1613    }
1614 }
1615
1616 static void free_signature(r_ctx &rctx)
1617 {
1618    if (rctx.sig) {
1619       crypto_sign_free(rctx.sig);
1620       rctx.sig = NULL;
1621    }
1622 }
1623
1624 static void free_session(r_ctx &rctx)
1625 {
1626    if (rctx.cs) {
1627       crypto_session_free(rctx.cs);
1628       rctx.cs = NULL;
1629    }
1630 }
1631
1632 /*
1633  * Verify the signature for the last restored file
1634  * Return value is either true (signature correct)
1635  * or false (signature could not be verified).
1636  * TODO landonf: Implement without using find_one_file and
1637  * without re-reading the file.
1638  */
1639 static bool verify_signature(r_ctx &rctx)
1640 {
1641    JCR *jcr = rctx.jcr;
1642    X509_KEYPAIR *keypair;
1643    DIGEST *digest = NULL;
1644    crypto_error_t err;
1645    uint64_t saved_bytes;
1646    crypto_digest_t signing_algorithm = have_sha2 ?
1647                                        CRYPTO_DIGEST_SHA256 : CRYPTO_DIGEST_SHA1;
1648    crypto_digest_t algorithm;
1649    SIGNATURE *sig = rctx.sig;
1650
1651
1652    if (!jcr->crypto.pki_sign) {
1653       /* no signature OK */
1654       return true;
1655    }
1656    if (!sig) {
1657       if (rctx.type == FT_REGE || rctx.type == FT_REG || rctx.type == FT_RAW) {
1658          Jmsg1(jcr, M_ERROR, 0, _("Missing cryptographic signature for %s\n"),
1659                jcr->last_fname);
1660          goto get_out;
1661       }
1662       return true;
1663    }
1664
1665    /* Iterate through the trusted signers */
1666    foreach_alist(keypair, jcr->crypto.pki_signers) {
1667       err = crypto_sign_get_digest(sig, jcr->crypto.pki_keypair, algorithm, &digest);
1668       switch (err) {
1669       case CRYPTO_ERROR_NONE:
1670          Dmsg0(50, "== Got digest\n");
1671          /*
1672           * We computed jcr->crypto.digest using signing_algorithm while writing
1673           * the file. If it is not the same as the algorithm used for
1674           * this file, punt by releasing the computed algorithm and
1675           * computing by re-reading the file.
1676           */
1677          if (algorithm != signing_algorithm) {
1678             if (jcr->crypto.digest) {
1679                crypto_digest_free(jcr->crypto.digest);
1680                jcr->crypto.digest = NULL;
1681             }
1682          }
1683          if (jcr->crypto.digest) {
1684             /* Use digest computed while writing the file to verify
1685              *  the signature */
1686             if ((err = crypto_sign_verify(sig, keypair, jcr->crypto.digest)) != CRYPTO_ERROR_NONE) {
1687                Dmsg1(50, "Bad signature on %s\n", jcr->last_fname);
1688                Jmsg2(jcr, M_ERROR, 0, _("Signature validation failed for file %s: ERR=%s\n"),
1689                      jcr->last_fname, crypto_strerror(err));
1690                goto get_out;
1691             }
1692          } else {
1693             /* Signature found, digest allocated.  Old method,
1694              *  re-read the file and compute the digest */
1695             jcr->crypto.digest = digest;
1696
1697             /* Checksum the entire file
1698              * Make sure we don't modify JobBytes by saving and
1699              *  restoring it */
1700             saved_bytes = jcr->JobBytes;
1701             if (find_one_file(jcr, jcr->ff, do_file_digest, jcr->last_fname, (dev_t)-1, 1) != 0) {
1702                Jmsg(jcr, M_ERROR, 0, _("Digest one file failed for file: %s\n"),
1703                     jcr->last_fname);
1704                jcr->JobBytes = saved_bytes;
1705                goto get_out;
1706             }
1707             jcr->JobBytes = saved_bytes;
1708
1709             /* Verify the signature */
1710             if ((err = crypto_sign_verify(sig, keypair, digest)) != CRYPTO_ERROR_NONE) {
1711                Dmsg1(50, "Bad signature on %s\n", jcr->last_fname);
1712                Jmsg2(jcr, M_ERROR, 0, _("Signature validation failed for file %s: ERR=%s\n"),
1713                      jcr->last_fname, crypto_strerror(err));
1714                goto get_out;
1715             }
1716             jcr->crypto.digest = NULL;
1717          }
1718
1719          /* Valid signature */
1720          Dmsg1(50, "Signature good on %s\n", jcr->last_fname);
1721          crypto_digest_free(digest);
1722          return true;
1723
1724       case CRYPTO_ERROR_NOSIGNER:
1725          /* Signature not found, try again */
1726          if (digest) {
1727             crypto_digest_free(digest);
1728             digest = NULL;
1729          }
1730          continue;
1731       default:
1732          /* Something strange happened (that shouldn't happen!)... */
1733          Qmsg2(jcr, M_ERROR, 0, _("Signature validation failed for %s: %s\n"), jcr->last_fname, crypto_strerror(err));
1734          goto get_out;
1735       }
1736    }
1737
1738    /* No signer */
1739    Dmsg1(50, "Could not find a valid public key for signature on %s\n", jcr->last_fname);
1740
1741 get_out:
1742    if (digest) {
1743       crypto_digest_free(digest);
1744    }
1745    return false;
1746 }