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