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