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