]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/backup.c
Pull compiler warning cleanup code + other from master
[bacula/bacula] / bacula / src / filed / backup.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2011 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 three of the GNU Affero 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 Affero 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 Kern Sibbald.
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  backup.c  send file attributes and data
30  *   to the Storage daemon.
31  *
32  *    Kern Sibbald, March MM
33  *
34  */
35
36 #include "bacula.h"
37 #include "filed.h"
38 #include "ch.h"
39
40 #ifdef HAVE_DARWIN_OS
41 const bool have_darwin_os = true;
42 #else
43 const bool have_darwin_os = false;
44 #endif
45
46 #if defined(HAVE_ACL)
47 const bool have_acl = true;
48 #else
49 const bool have_acl = false;
50 #endif
51
52 #if defined(HAVE_XATTR)
53 const bool have_xattr = true;
54 #else
55 const bool have_xattr = false;
56 #endif
57
58 /* Forward referenced functions */
59 int save_file(JCR *jcr, FF_PKT *ff_pkt, bool top_level);
60 static int send_data(JCR *jcr, int stream, FF_PKT *ff_pkt, DIGEST *digest, DIGEST *signature_digest);
61 bool encode_and_send_attributes(JCR *jcr, FF_PKT *ff_pkt, int &data_stream);
62 static bool crypto_session_start(JCR *jcr);
63 static void crypto_session_end(JCR *jcr);
64 static bool crypto_session_send(JCR *jcr, BSOCK *sd);
65 static void close_vss_backup_session(JCR *jcr);
66
67 /**
68  * Find all the requested files and send them
69  * to the Storage daemon.
70  *
71  * Note, we normally carry on a one-way
72  * conversation from this point on with the SD, simply blasting
73  * data to him.  To properly know what is going on, we
74  * also run a "heartbeat" monitor which reads the socket and
75  * reacts accordingly (at the moment it has nothing to do
76  * except echo the heartbeat to the Director).
77  *
78  */
79 bool blast_data_to_storage_daemon(JCR *jcr, char *addr)
80 {
81    BSOCK *sd;
82    bool ok = true;
83    // TODO landonf: Allow user to specify encryption algorithm
84
85    sd = jcr->store_bsock;
86
87    jcr->setJobStatus(JS_Running);
88
89    Dmsg1(300, "bfiled: opened data connection %d to stored\n", sd->m_fd);
90
91    LockRes();
92    CLIENT *client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
93    UnlockRes();
94    uint32_t buf_size;
95    if (client) {
96       buf_size = client->max_network_buffer_size;
97    } else {
98       buf_size = 0;                   /* use default */
99    }
100    if (!sd->set_buffer_size(buf_size, BNET_SETBUF_WRITE)) {
101       jcr->setJobStatus(JS_ErrorTerminated);
102       Jmsg(jcr, M_FATAL, 0, _("Cannot set buffer size FD->SD.\n"));
103       return false;
104    }
105
106    jcr->buf_size = sd->msglen;
107    /**
108     * Adjust for compression so that output buffer is
109     *  12 bytes + 0.1% larger than input buffer plus 18 bytes.
110     *  This gives a bit extra plus room for the sparse addr if any.
111     *  Note, we adjust the read size to be smaller so that the
112     *  same output buffer can be used without growing it.
113     *
114     *  For LZO1X compression the recommended value is :
115     *                  output_block_size = input_block_size + (input_block_size / 16) + 64 + 3 + sizeof(comp_stream_header)
116     *
117     * The zlib compression workset is initialized here to minimize
118     *  the "per file" load. The jcr member is only set, if the init 
119     *  was successful.
120     *
121     *  For the same reason, lzo compression is initialized here.
122     */
123 #ifdef HAVE_LZO
124    jcr->compress_buf_size = MAX(jcr->buf_size + (jcr->buf_size / 16) + 67 + (int)sizeof(comp_stream_header), jcr->buf_size + ((jcr->buf_size+999) / 1000) + 30);
125    jcr->compress_buf = get_memory(jcr->compress_buf_size);
126 #else
127    jcr->compress_buf_size = jcr->buf_size + ((jcr->buf_size+999) / 1000) + 30;
128    jcr->compress_buf = get_memory(jcr->compress_buf_size);
129 #endif
130    
131 #ifdef HAVE_LIBZ
132    z_stream *pZlibStream = (z_stream*)malloc(sizeof(z_stream));  
133    if (pZlibStream) {
134       pZlibStream->zalloc = Z_NULL;      
135       pZlibStream->zfree = Z_NULL;
136       pZlibStream->opaque = Z_NULL;
137       pZlibStream->state = Z_NULL;
138
139       if (deflateInit(pZlibStream, Z_DEFAULT_COMPRESSION) == Z_OK) {
140          jcr->pZLIB_compress_workset = pZlibStream;
141       } else {
142          free (pZlibStream);
143       }
144    }
145 #endif
146
147 #ifdef HAVE_LZO
148    lzo_voidp pLzoMem = (lzo_voidp) malloc(LZO1X_1_MEM_COMPRESS);
149    if (pLzoMem) {
150       if (lzo_init() == LZO_E_OK) {
151          jcr->LZO_compress_workset = pLzoMem;
152       } else {
153          free (pLzoMem);
154       }
155    }
156 #endif
157
158    if (!crypto_session_start(jcr)) {
159       return false;
160    }
161
162    set_find_options((FF_PKT *)jcr->ff, jcr->incremental, jcr->mtime);
163
164    /** in accurate mode, we overload the find_one check function */
165    if (jcr->accurate) {
166       set_find_changed_function((FF_PKT *)jcr->ff, accurate_check_file);
167    } 
168    
169    start_heartbeat_monitor(jcr);
170
171    if (have_acl) {
172       jcr->acl_data = (acl_data_t *)malloc(sizeof(acl_data_t));
173       memset((caddr_t)jcr->acl_data, 0, sizeof(acl_data_t));
174       jcr->acl_data->content = get_pool_memory(PM_MESSAGE);
175    }
176
177    if (have_xattr) {
178       jcr->xattr_data = (xattr_data_t *)malloc(sizeof(xattr_data_t));
179       memset((caddr_t)jcr->xattr_data, 0, sizeof(xattr_data_t));
180       jcr->xattr_data->content = get_pool_memory(PM_MESSAGE);
181    }
182
183    /** Subroutine save_file() is called for each file */
184    if (!find_files(jcr, (FF_PKT *)jcr->ff, save_file, plugin_save)) {
185       ok = false;                     /* error */
186       jcr->setJobStatus(JS_ErrorTerminated);
187    }
188
189    if (have_acl && jcr->acl_data->nr_errors > 0) {
190       Jmsg(jcr, M_WARNING, 0, _("Encountered %ld acl errors while doing backup\n"),
191            jcr->acl_data->nr_errors);
192    }
193    if (have_xattr && jcr->xattr_data->nr_errors > 0) {
194       Jmsg(jcr, M_WARNING, 0, _("Encountered %ld xattr errors while doing backup\n"),
195            jcr->xattr_data->nr_errors);
196    }
197
198    close_vss_backup_session(jcr);
199
200    accurate_finish(jcr);              /* send deleted or base file list to SD */
201
202    stop_heartbeat_monitor(jcr);
203
204    sd->signal(BNET_EOD);            /* end of sending data */
205
206    if (have_acl && jcr->acl_data) {
207       free_pool_memory(jcr->acl_data->content);
208       free(jcr->acl_data);
209       jcr->acl_data = NULL;
210    }
211    if (have_xattr && jcr->xattr_data) {
212       free_pool_memory(jcr->xattr_data->content);
213       free(jcr->xattr_data);
214       jcr->xattr_data = NULL;
215    }
216    if (jcr->big_buf) {
217       free(jcr->big_buf);
218       jcr->big_buf = NULL;
219    }
220    if (jcr->compress_buf) {
221       free_pool_memory(jcr->compress_buf);
222       jcr->compress_buf = NULL;
223    }
224    if (jcr->pZLIB_compress_workset) {
225       /* Free the zlib stream */
226 #ifdef HAVE_LIBZ
227       deflateEnd((z_stream *)jcr->pZLIB_compress_workset);
228 #endif
229       free (jcr->pZLIB_compress_workset);
230       jcr->pZLIB_compress_workset = NULL;
231    }
232    if (jcr->LZO_compress_workset) {
233       free (jcr->LZO_compress_workset);
234       jcr->LZO_compress_workset = NULL;
235    }
236
237    crypto_session_end(jcr);
238
239
240    Dmsg1(100, "end blast_data ok=%d\n", ok);
241    return ok;
242 }
243
244 static bool crypto_session_start(JCR *jcr)
245 {
246    crypto_cipher_t cipher = CRYPTO_CIPHER_AES_128_CBC;
247
248    /**
249     * Create encryption session data and a cached, DER-encoded session data
250     * structure. We use a single session key for each backup, so we'll encode
251     * the session data only once.
252     */
253    if (jcr->crypto.pki_encrypt) {
254       uint32_t size = 0;
255
256       /** Create per-job session encryption context */
257       jcr->crypto.pki_session = crypto_session_new(cipher, jcr->crypto.pki_recipients);
258
259       /** Get the session data size */
260       if (!crypto_session_encode(jcr->crypto.pki_session, (uint8_t *)0, &size)) {
261          Jmsg(jcr, M_FATAL, 0, _("An error occurred while encrypting the stream.\n"));
262          return false;
263       }
264
265       /** Allocate buffer */
266       jcr->crypto.pki_session_encoded = get_memory(size);
267
268       /** Encode session data */
269       if (!crypto_session_encode(jcr->crypto.pki_session, (uint8_t *)jcr->crypto.pki_session_encoded, &size)) {
270          Jmsg(jcr, M_FATAL, 0, _("An error occurred while encrypting the stream.\n"));
271          return false;
272       }
273
274       /** ... and store the encoded size */
275       jcr->crypto.pki_session_encoded_size = size;
276
277       /** Allocate the encryption/decryption buffer */
278       jcr->crypto.crypto_buf = get_memory(CRYPTO_CIPHER_MAX_BLOCK_SIZE);
279    }
280    return true;
281 }
282
283 static void crypto_session_end(JCR *jcr)
284 {
285    if (jcr->crypto.crypto_buf) {
286       free_pool_memory(jcr->crypto.crypto_buf);
287       jcr->crypto.crypto_buf = NULL;
288    }
289    if (jcr->crypto.pki_session) {
290       crypto_session_free(jcr->crypto.pki_session);
291    }
292    if (jcr->crypto.pki_session_encoded) {
293       free_pool_memory(jcr->crypto.pki_session_encoded);
294       jcr->crypto.pki_session_encoded = NULL;
295    }
296 }
297
298 static bool crypto_session_send(JCR *jcr, BSOCK *sd)
299 {
300    POOLMEM *msgsave;
301
302    /** Send our header */
303    Dmsg2(100, "Send hdr fi=%ld stream=%d\n", jcr->JobFiles, STREAM_ENCRYPTED_SESSION_DATA);
304    sd->fsend("%ld %d 0", jcr->JobFiles, STREAM_ENCRYPTED_SESSION_DATA);
305
306    msgsave = sd->msg;
307    sd->msg = jcr->crypto.pki_session_encoded;
308    sd->msglen = jcr->crypto.pki_session_encoded_size;
309    jcr->JobBytes += sd->msglen;
310
311    Dmsg1(100, "Send data len=%d\n", sd->msglen);
312    sd->send();
313    sd->msg = msgsave;
314    sd->signal(BNET_EOD);
315    return true;
316 }
317
318
319 /**
320  * Called here by find() for each file included.
321  *   This is a callback. The original is find_files() above.
322  *
323  *  Send the file and its data to the Storage daemon.
324  *
325  *  Returns: 1 if OK
326  *           0 if error
327  *          -1 to ignore file/directory (not used here)
328  */
329 int save_file(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
330 {
331    bool do_read = false;
332    bool plugin_started = false;
333    int stat, data_stream; 
334    int rtnstat = 0;
335    DIGEST *digest = NULL;
336    DIGEST *signing_digest = NULL;
337    int digest_stream = STREAM_NONE;
338    SIGNATURE *sig = NULL;
339    bool has_file_data = false;
340    // TODO landonf: Allow the user to specify the digest algorithm
341 #ifdef HAVE_SHA2
342    crypto_digest_t signing_algorithm = CRYPTO_DIGEST_SHA256;
343 #else
344    crypto_digest_t signing_algorithm = CRYPTO_DIGEST_SHA1;
345 #endif
346    BSOCK *sd = jcr->store_bsock;
347
348    if (jcr->is_canceled() || jcr->is_incomplete()) {
349       return 0;
350    }
351
352    jcr->num_files_examined++;         /* bump total file count */
353
354    switch (ff_pkt->type) {
355    case FT_LNKSAVED:                  /* Hard linked, file already saved */
356       Dmsg2(130, "FT_LNKSAVED hard link: %s => %s\n", ff_pkt->fname, ff_pkt->link);
357       break;
358    case FT_REGE:
359       Dmsg1(130, "FT_REGE saving: %s\n", ff_pkt->fname);
360       has_file_data = true;
361       break;
362    case FT_REG:
363       Dmsg1(130, "FT_REG saving: %s\n", ff_pkt->fname);
364       has_file_data = true;
365       break;
366    case FT_LNK:
367       Dmsg2(130, "FT_LNK saving: %s -> %s\n", ff_pkt->fname, ff_pkt->link);
368       break;
369    case FT_RESTORE_FIRST:
370       Dmsg1(100, "FT_RESTORE_FIRST saving: %s\n", ff_pkt->fname);
371       break;
372    case FT_DIRBEGIN:
373       jcr->num_files_examined--;      /* correct file count */
374       return 1;                       /* not used */
375    case FT_NORECURSE:
376       Jmsg(jcr, M_INFO, 1, _("     Recursion turned off. Will not descend from %s into %s\n"),
377            ff_pkt->top_fname, ff_pkt->fname);
378       ff_pkt->type = FT_DIREND;       /* Backup only the directory entry */
379       break;
380    case FT_NOFSCHG:
381       /* Suppress message for /dev filesystems */
382       if (!is_in_fileset(ff_pkt)) {
383          Jmsg(jcr, M_INFO, 1, _("     %s is a different filesystem. Will not descend from %s into it.\n"),
384               ff_pkt->fname, ff_pkt->top_fname);
385       }
386       ff_pkt->type = FT_DIREND;       /* Backup only the directory entry */
387       break;
388    case FT_INVALIDFS:
389       Jmsg(jcr, M_INFO, 1, _("     Disallowed filesystem. Will not descend from %s into %s\n"),
390            ff_pkt->top_fname, ff_pkt->fname);
391       ff_pkt->type = FT_DIREND;       /* Backup only the directory entry */
392       break;
393    case FT_INVALIDDT:
394       Jmsg(jcr, M_INFO, 1, _("     Disallowed drive type. Will not descend into %s\n"),
395            ff_pkt->fname);
396       break;
397    case FT_REPARSE:
398    case FT_JUNCTION:
399    case FT_DIREND:
400       Dmsg1(130, "FT_DIREND: %s\n", ff_pkt->link);
401       break;
402    case FT_SPEC:
403       Dmsg1(130, "FT_SPEC saving: %s\n", ff_pkt->fname);
404       if (S_ISSOCK(ff_pkt->statp.st_mode)) {
405         Jmsg(jcr, M_SKIPPED, 1, _("     Socket file skipped: %s\n"), ff_pkt->fname);
406         return 1;
407       }
408       break;
409    case FT_RAW:
410       Dmsg1(130, "FT_RAW saving: %s\n", ff_pkt->fname);
411       has_file_data = true;
412       break;
413    case FT_FIFO:
414       Dmsg1(130, "FT_FIFO saving: %s\n", ff_pkt->fname);
415       break;
416    case FT_NOACCESS: {
417       berrno be;
418       Jmsg(jcr, M_NOTSAVED, 0, _("     Could not access \"%s\": ERR=%s\n"), ff_pkt->fname,
419          be.bstrerror(ff_pkt->ff_errno));
420       jcr->JobErrors++;
421       return 1;
422    }
423    case FT_NOFOLLOW: {
424       berrno be;
425       Jmsg(jcr, M_NOTSAVED, 0, _("     Could not follow link \"%s\": ERR=%s\n"), 
426            ff_pkt->fname, be.bstrerror(ff_pkt->ff_errno));
427       jcr->JobErrors++;
428       return 1;
429    }
430    case FT_NOSTAT: {
431       berrno be;
432       Jmsg(jcr, M_NOTSAVED, 0, _("     Could not stat \"%s\": ERR=%s\n"), ff_pkt->fname,
433          be.bstrerror(ff_pkt->ff_errno));
434       jcr->JobErrors++;
435       return 1;
436    }
437    case FT_DIRNOCHG:
438    case FT_NOCHG:
439       Jmsg(jcr, M_SKIPPED, 1, _("     Unchanged file skipped: %s\n"), ff_pkt->fname);
440       return 1;
441    case FT_ISARCH:
442       Jmsg(jcr, M_NOTSAVED, 0, _("     Archive file not saved: %s\n"), ff_pkt->fname);
443       return 1;
444    case FT_NOOPEN: {
445       berrno be;
446       Jmsg(jcr, M_NOTSAVED, 0, _("     Could not open directory \"%s\": ERR=%s\n"), 
447            ff_pkt->fname, be.bstrerror(ff_pkt->ff_errno));
448       jcr->JobErrors++;
449       return 1;
450    }
451    default:
452       Jmsg(jcr, M_NOTSAVED, 0,  _("     Unknown file type %d; not saved: %s\n"), 
453            ff_pkt->type, ff_pkt->fname);
454       jcr->JobErrors++;
455       return 1;
456    }
457
458    Dmsg1(130, "bfiled: sending %s to stored\n", ff_pkt->fname);
459
460    /** Digests and encryption are only useful if there's file data */
461    if (has_file_data) {
462       /**
463        * Setup for digest handling. If this fails, the digest will be set to NULL
464        * and not used. Note, the digest (file hash) can be any one of the four
465        * algorithms below.
466        *
467        * The signing digest is a single algorithm depending on
468        * whether or not we have SHA2.              
469        *   ****FIXME****  the signing algoritm should really be
470        *   determined a different way!!!!!!  What happens if
471        *   sha2 was available during backup but not restore?
472        */
473       if (ff_pkt->flags & FO_MD5) {
474          digest = crypto_digest_new(jcr, CRYPTO_DIGEST_MD5);
475          digest_stream = STREAM_MD5_DIGEST;
476
477       } else if (ff_pkt->flags & FO_SHA1) {
478          digest = crypto_digest_new(jcr, CRYPTO_DIGEST_SHA1);
479          digest_stream = STREAM_SHA1_DIGEST;
480
481       } else if (ff_pkt->flags & FO_SHA256) {
482          digest = crypto_digest_new(jcr, CRYPTO_DIGEST_SHA256);
483          digest_stream = STREAM_SHA256_DIGEST;
484
485       } else if (ff_pkt->flags & FO_SHA512) {
486          digest = crypto_digest_new(jcr, CRYPTO_DIGEST_SHA512);
487          digest_stream = STREAM_SHA512_DIGEST;
488       }
489
490       /** Did digest initialization fail? */
491       if (digest_stream != STREAM_NONE && digest == NULL) {
492          Jmsg(jcr, M_WARNING, 0, _("%s digest initialization failed\n"),
493             stream_to_ascii(digest_stream));
494       }
495
496       /**
497        * Set up signature digest handling. If this fails, the signature digest
498        * will be set to NULL and not used.
499        */
500       /* TODO landonf: We should really only calculate the digest once, for
501        * both verification and signing.
502        */
503       if (jcr->crypto.pki_sign) {
504          signing_digest = crypto_digest_new(jcr, signing_algorithm);
505
506          /** Full-stop if a failure occurred initializing the signature digest */
507          if (signing_digest == NULL) {
508             Jmsg(jcr, M_NOTSAVED, 0, _("%s signature digest initialization failed\n"),
509                stream_to_ascii(signing_algorithm));
510             jcr->JobErrors++;
511             goto good_rtn;
512          }
513       }
514
515       /** Enable encryption */
516       if (jcr->crypto.pki_encrypt) {
517          ff_pkt->flags |= FO_ENCRYPT;
518       }
519    }
520
521    /** Initialize the file descriptor we use for data and other streams. */
522    binit(&ff_pkt->bfd);
523    if (ff_pkt->flags & FO_PORTABLE) {
524       set_portable_backup(&ff_pkt->bfd); /* disable Win32 BackupRead() */
525    }
526    if (ff_pkt->cmd_plugin) {
527       /* Tell bfile that it needs to call plugin */
528       if (!set_cmd_plugin(&ff_pkt->bfd, jcr)) {
529          goto bail_out;
530       }
531       send_plugin_name(jcr, sd, true);      /* signal start of plugin data */
532       plugin_started = true;
533    }
534
535    /** Send attributes -- must be done after binit() */
536    if (!encode_and_send_attributes(jcr, ff_pkt, data_stream)) {
537       goto bail_out;
538    }
539    /** Meta data only for restore object */
540    if (ff_pkt->type == FT_RESTORE_FIRST) {
541       goto good_rtn;
542    }
543
544    /** Set up the encryption context and send the session data to the SD */
545    if (has_file_data && jcr->crypto.pki_encrypt) {
546       if (!crypto_session_send(jcr, sd)) {
547          goto bail_out;
548       }
549    }
550
551    /**
552     * Open any file with data that we intend to save, then save it.
553     *
554     * Note, if is_win32_backup, we must open the Directory so that
555     * the BackupRead will save its permissions and ownership streams.
556     */
557    if (ff_pkt->type != FT_LNKSAVED && S_ISREG(ff_pkt->statp.st_mode)) {
558 #ifdef HAVE_WIN32
559       do_read = !is_portable_backup(&ff_pkt->bfd) || ff_pkt->statp.st_size > 0;
560 #else
561       do_read = ff_pkt->statp.st_size > 0;  
562 #endif
563    } else if (ff_pkt->type == FT_RAW || ff_pkt->type == FT_FIFO ||
564               ff_pkt->type == FT_REPARSE || ff_pkt->type == FT_JUNCTION ||
565          (!is_portable_backup(&ff_pkt->bfd) && ff_pkt->type == FT_DIREND)) {
566       do_read = true;
567    }
568
569    if (ff_pkt->cmd_plugin) {
570       do_read = true;
571    }
572
573    Dmsg2(150, "type=%d do_read=%d\n", ff_pkt->type, do_read);
574    if (do_read) {
575       btimer_t *tid;
576
577       if (ff_pkt->type == FT_FIFO) {
578          tid = start_thread_timer(jcr, pthread_self(), 60);
579       } else {
580          tid = NULL;
581       }
582       int noatime = ff_pkt->flags & FO_NOATIME ? O_NOATIME : 0;
583       ff_pkt->bfd.reparse_point = (ff_pkt->type == FT_REPARSE || 
584                                    ff_pkt->type == FT_JUNCTION);
585       if (bopen(&ff_pkt->bfd, ff_pkt->fname, O_RDONLY | O_BINARY | noatime, 0) < 0) {
586          ff_pkt->ff_errno = errno;
587          berrno be;
588          Jmsg(jcr, M_NOTSAVED, 0, _("     Cannot open \"%s\": ERR=%s.\n"), ff_pkt->fname,
589               be.bstrerror());
590          jcr->JobErrors++;
591          if (tid) {
592             stop_thread_timer(tid);
593             tid = NULL;
594          }
595          goto good_rtn;
596       }
597       if (tid) {
598          stop_thread_timer(tid);
599          tid = NULL;
600       }
601
602       stat = send_data(jcr, data_stream, ff_pkt, digest, signing_digest);
603
604       if (ff_pkt->flags & FO_CHKCHANGES) {
605          has_file_changed(jcr, ff_pkt);
606       }
607
608       bclose(&ff_pkt->bfd);
609       
610       if (!stat) {
611          goto bail_out;
612       }
613    }
614
615    if (have_darwin_os) {
616       /** Regular files can have resource forks and Finder Info */
617       if (ff_pkt->type != FT_LNKSAVED && (S_ISREG(ff_pkt->statp.st_mode) &&
618           ff_pkt->flags & FO_HFSPLUS)) {
619          if (ff_pkt->hfsinfo.rsrclength > 0) {
620             int flags;
621             int rsrc_stream;
622             if (!bopen_rsrc(&ff_pkt->bfd, ff_pkt->fname, O_RDONLY | O_BINARY, 0) < 0) {
623                ff_pkt->ff_errno = errno;
624                berrno be;
625                Jmsg(jcr, M_NOTSAVED, -1, _("     Cannot open resource fork for \"%s\": ERR=%s.\n"),
626                     ff_pkt->fname, be.bstrerror());
627                jcr->JobErrors++;
628                if (is_bopen(&ff_pkt->bfd)) {
629                   bclose(&ff_pkt->bfd);
630                }
631                goto good_rtn;
632             }
633             flags = ff_pkt->flags;
634             ff_pkt->flags &= ~(FO_COMPRESS|FO_SPARSE|FO_OFFSETS);
635             if (flags & FO_ENCRYPT) {
636                rsrc_stream = STREAM_ENCRYPTED_MACOS_FORK_DATA;
637             } else {
638                rsrc_stream = STREAM_MACOS_FORK_DATA;
639             }
640             stat = send_data(jcr, rsrc_stream, ff_pkt, digest, signing_digest);
641             ff_pkt->flags = flags;
642             bclose(&ff_pkt->bfd);
643             if (!stat) {
644                goto bail_out;
645             }
646          }
647
648          Dmsg1(300, "Saving Finder Info for \"%s\"\n", ff_pkt->fname);
649          sd->fsend("%ld %d 0", jcr->JobFiles, STREAM_HFSPLUS_ATTRIBUTES);
650          Dmsg1(300, "bfiled>stored:header %s\n", sd->msg);
651          pm_memcpy(sd->msg, ff_pkt->hfsinfo.fndrinfo, 32);
652          sd->msglen = 32;
653          if (digest) {
654             crypto_digest_update(digest, (uint8_t *)sd->msg, sd->msglen);
655          }
656          if (signing_digest) {
657             crypto_digest_update(signing_digest, (uint8_t *)sd->msg, sd->msglen);
658          }
659          sd->send();
660          sd->signal(BNET_EOD);
661       }
662    }
663
664    /**
665     * Save ACLs when requested and available for anything not being a symlink and not being a plugin.
666     */
667    if (have_acl) {
668       if (ff_pkt->flags & FO_ACL && ff_pkt->type != FT_LNK && !ff_pkt->cmd_plugin) {
669          switch (build_acl_streams(jcr, ff_pkt)) {
670          case bacl_exit_fatal:
671             goto bail_out;
672          case bacl_exit_error:
673             /**
674              * Non-fatal errors, count them and when the number is under ACL_REPORT_ERR_MAX_PER_JOB
675              * print the error message set by the lower level routine in jcr->errmsg.
676              */
677             if (jcr->acl_data->nr_errors < ACL_REPORT_ERR_MAX_PER_JOB) {
678                Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
679             }
680             jcr->acl_data->nr_errors++;
681             break;
682          case bacl_exit_ok:
683             break;
684          }
685       }
686    }
687
688    /**
689     * Save Extended Attributes when requested and available for all files not being a plugin.
690     */
691    if (have_xattr) {
692       if (ff_pkt->flags & FO_XATTR && !ff_pkt->cmd_plugin) {
693          switch (build_xattr_streams(jcr, ff_pkt)) {
694          case bxattr_exit_fatal:
695             goto bail_out;
696          case bxattr_exit_error:
697             /**
698              * Non-fatal errors, count them and when the number is under XATTR_REPORT_ERR_MAX_PER_JOB
699              * print the error message set by the lower level routine in jcr->errmsg.
700              */
701             if (jcr->xattr_data->nr_errors < XATTR_REPORT_ERR_MAX_PER_JOB) {
702                Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
703             }
704             jcr->xattr_data->nr_errors++;
705             break;
706          case bxattr_exit_ok:
707             break;
708          }
709       }
710    }
711
712    /** Terminate the signing digest and send it to the Storage daemon */
713    if (signing_digest) {
714       uint32_t size = 0;
715
716       if ((sig = crypto_sign_new(jcr)) == NULL) {
717          Jmsg(jcr, M_FATAL, 0, _("Failed to allocate memory for crypto signature.\n"));
718          goto bail_out;
719       }
720
721       if (!crypto_sign_add_signer(sig, signing_digest, jcr->crypto.pki_keypair)) {
722          Jmsg(jcr, M_FATAL, 0, _("An error occurred while signing the stream.\n"));
723          goto bail_out;
724       }
725
726       /** Get signature size */
727       if (!crypto_sign_encode(sig, NULL, &size)) {
728          Jmsg(jcr, M_FATAL, 0, _("An error occurred while signing the stream.\n"));
729          goto bail_out;
730       }
731
732       /** Grow the bsock buffer to fit our message if necessary */
733       if (sizeof_pool_memory(sd->msg) < (int32_t)size) {
734          sd->msg = realloc_pool_memory(sd->msg, size);
735       }
736
737       /** Send our header */
738       sd->fsend("%ld %ld 0", jcr->JobFiles, STREAM_SIGNED_DIGEST);
739       Dmsg1(300, "bfiled>stored:header %s\n", sd->msg);
740
741       /** Encode signature data */
742       if (!crypto_sign_encode(sig, (uint8_t *)sd->msg, &size)) {
743          Jmsg(jcr, M_FATAL, 0, _("An error occurred while signing the stream.\n"));
744          goto bail_out;
745       }
746
747       sd->msglen = size;
748       sd->send();
749       sd->signal(BNET_EOD);              /* end of checksum */
750    }
751
752    /** Terminate any digest and send it to Storage daemon */
753    if (digest) {
754       uint32_t size;
755
756       sd->fsend("%ld %d 0", jcr->JobFiles, digest_stream);
757       Dmsg1(300, "bfiled>stored:header %s\n", sd->msg);
758
759       size = CRYPTO_DIGEST_MAX_SIZE;
760
761       /** Grow the bsock buffer to fit our message if necessary */
762       if (sizeof_pool_memory(sd->msg) < (int32_t)size) {
763          sd->msg = realloc_pool_memory(sd->msg, size);
764       }
765
766       if (!crypto_digest_finalize(digest, (uint8_t *)sd->msg, &size)) {
767          Jmsg(jcr, M_FATAL, 0, _("An error occurred finalizing signing the stream.\n"));
768          goto bail_out;
769       }
770
771       /* Keep the checksum if this file is a hardlink */
772       if (ff_pkt->linked) {
773          ff_pkt_set_link_digest(ff_pkt, digest_stream, sd->msg, size);
774       }
775
776       sd->msglen = size;
777       sd->send();
778       sd->signal(BNET_EOD);              /* end of checksum */
779    }
780
781    /* Check if original file has a digest, and send it */
782    if (ff_pkt->type == FT_LNKSAVED && ff_pkt->digest) {
783       Dmsg2(300, "Link %s digest %d\n", ff_pkt->fname, ff_pkt->digest_len);
784       sd->fsend("%ld %d 0", jcr->JobFiles, ff_pkt->digest_stream);
785
786       sd->msg = check_pool_memory_size(sd->msg, ff_pkt->digest_len);
787       memcpy(sd->msg, ff_pkt->digest, ff_pkt->digest_len);
788       sd->msglen = ff_pkt->digest_len;
789       sd->send();
790
791       sd->signal(BNET_EOD);              /* end of hardlink record */
792    }
793
794 good_rtn:
795    rtnstat = jcr->is_canceled() ? 0 : 1; /* good return if not canceled */
796
797 bail_out:
798    if (jcr->is_incomplete()) {
799       rtnstat = 0;
800    }
801    if (ff_pkt->cmd_plugin && plugin_started) {
802       send_plugin_name(jcr, sd, false); /* signal end of plugin data */
803    }
804    if (digest) {
805       crypto_digest_free(digest);
806    }
807    if (signing_digest) {
808       crypto_digest_free(signing_digest);
809    }
810    if (sig) {
811       crypto_sign_free(sig);        
812    }
813    return rtnstat;
814 }
815
816 /**
817  * Send data read from an already open file descriptor.
818  *
819  * We return 1 on sucess and 0 on errors.
820  *
821  * ***FIXME***
822  * We use ff_pkt->statp.st_size when FO_SPARSE to know when to stop
823  *  reading.
824  * Currently this is not a problem as the only other stream, resource forks,
825  * are not handled as sparse files.
826  */
827 static int send_data(JCR *jcr, int stream, FF_PKT *ff_pkt, DIGEST *digest, 
828                      DIGEST *signing_digest)
829 {
830    BSOCK *sd = jcr->store_bsock;
831    uint64_t fileAddr = 0;             /* file address */
832    char *rbuf, *wbuf;
833    int32_t rsize = jcr->buf_size;      /* read buffer size */
834    POOLMEM *msgsave;
835    CIPHER_CONTEXT *cipher_ctx = NULL; /* Quell bogus uninitialized warnings */
836    const uint8_t *cipher_input;
837    uint32_t cipher_input_len;
838    uint32_t cipher_block_size;
839    uint32_t encrypted_len;
840 #ifdef FD_NO_SEND_TEST
841    return 1;
842 #endif
843
844    msgsave = sd->msg;
845    rbuf = sd->msg;                    /* read buffer */
846    wbuf = sd->msg;                    /* write buffer */
847    cipher_input = (uint8_t *)rbuf;    /* encrypt uncompressed data */
848
849    Dmsg1(300, "Saving data, type=%d\n", ff_pkt->type);
850
851 #if defined(HAVE_LIBZ) || defined(HAVE_LZO)
852    uLong compress_len = 0;
853    uLong max_compress_len = 0;
854    const Bytef *cbuf = NULL;
855  #ifdef HAVE_LIBZ
856    int zstat;
857
858    if ((ff_pkt->flags & FO_COMPRESS) && ff_pkt->Compress_algo == COMPRESS_GZIP) {
859       if ((ff_pkt->flags & FO_SPARSE) || (ff_pkt->flags & FO_OFFSETS)) {
860          cbuf = (Bytef *)jcr->compress_buf + OFFSET_FADDR_SIZE;
861          max_compress_len = jcr->compress_buf_size - OFFSET_FADDR_SIZE;
862       } else {
863          cbuf = (Bytef *)jcr->compress_buf;
864          max_compress_len = jcr->compress_buf_size; /* set max length */
865       }
866       wbuf = jcr->compress_buf;    /* compressed output here */
867       cipher_input = (uint8_t *)jcr->compress_buf; /* encrypt compressed data */
868
869       /**
870        * Only change zlib parameters if there is no pending operation.
871        * This should never happen as deflatereset is called after each
872        * deflate.
873        */
874
875       if (((z_stream*)jcr->pZLIB_compress_workset)->total_in == 0) {
876          /** set gzip compression level - must be done per file */
877          if ((zstat=deflateParams((z_stream*)jcr->pZLIB_compress_workset, 
878               ff_pkt->Compress_level, Z_DEFAULT_STRATEGY)) != Z_OK) {
879             Jmsg(jcr, M_FATAL, 0, _("Compression deflateParams error: %d\n"), zstat);
880             jcr->setJobStatus(JS_ErrorTerminated);
881             goto err;
882          }
883       }
884    }
885  #endif
886  #ifdef HAVE_LZO
887    Bytef *cbuf2;
888    int lzores;
889    comp_stream_header ch;
890
891    memset(&ch, 0, sizeof(comp_stream_header));
892    cbuf2 = NULL;
893
894    if ((ff_pkt->flags & FO_COMPRESS) && ff_pkt->Compress_algo == COMPRESS_LZO1X) {
895       if ((ff_pkt->flags & FO_SPARSE) || (ff_pkt->flags & FO_OFFSETS)) {
896          cbuf = (Bytef *)jcr->compress_buf + OFFSET_FADDR_SIZE;
897          cbuf2 = (Bytef *)jcr->compress_buf + OFFSET_FADDR_SIZE + sizeof(comp_stream_header);
898          max_compress_len = jcr->compress_buf_size - OFFSET_FADDR_SIZE;
899       } else {
900          cbuf = (Bytef *)jcr->compress_buf;
901          cbuf2 = (Bytef *)jcr->compress_buf + sizeof(comp_stream_header);
902          max_compress_len = jcr->compress_buf_size; /* set max length */
903       }
904       ch.magic = COMPRESS_LZO1X;
905       ch.version = COMP_HEAD_VERSION;
906       wbuf = jcr->compress_buf;    /* compressed output here */
907       cipher_input = (uint8_t *)jcr->compress_buf; /* encrypt compressed data */
908    }
909  #endif
910 #else
911    const uint32_t max_compress_len = 0;
912 #endif
913
914    if (ff_pkt->flags & FO_ENCRYPT) {
915       if ((ff_pkt->flags & FO_SPARSE) || (ff_pkt->flags & FO_OFFSETS)) {
916          Jmsg0(jcr, M_FATAL, 0, _("Encrypting sparse or offset data not supported.\n"));
917          goto err;
918       }
919       /** Allocate the cipher context */
920       if ((cipher_ctx = crypto_cipher_new(jcr->crypto.pki_session, true, 
921            &cipher_block_size)) == NULL) {
922          /* Shouldn't happen! */
923          Jmsg0(jcr, M_FATAL, 0, _("Failed to initialize encryption context.\n"));
924          goto err;
925       }
926
927       /**
928        * Grow the crypto buffer, if necessary.
929        * crypto_cipher_update() will buffer up to (cipher_block_size - 1).
930        * We grow crypto_buf to the maximum number of blocks that
931        * could be returned for the given read buffer size.
932        * (Using the larger of either rsize or max_compress_len)
933        */
934       jcr->crypto.crypto_buf = check_pool_memory_size(jcr->crypto.crypto_buf, 
935            (MAX(rsize + (int)sizeof(uint32_t), (int32_t)max_compress_len) + 
936             cipher_block_size - 1) / cipher_block_size * cipher_block_size);
937
938       wbuf = jcr->crypto.crypto_buf; /* Encrypted, possibly compressed output here. */
939    }
940
941    /**
942     * Send Data header to Storage daemon
943     *    <file-index> <stream> <info>
944     */
945    if (!sd->fsend("%ld %d 0", jcr->JobFiles, stream)) {
946       if (!jcr->is_job_canceled()) {
947          Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
948                sd->bstrerror());
949       }
950       goto err;
951    }
952    Dmsg1(300, ">stored: datahdr %s\n", sd->msg);
953
954    /**
955     * Make space at beginning of buffer for fileAddr because this
956     *   same buffer will be used for writing if compression is off.
957     */
958    if ((ff_pkt->flags & FO_SPARSE) || (ff_pkt->flags & FO_OFFSETS)) {
959       rbuf += OFFSET_FADDR_SIZE;
960       rsize -= OFFSET_FADDR_SIZE;
961 #ifdef HAVE_FREEBSD_OS
962       /**
963        * To read FreeBSD partitions, the read size must be
964        *  a multiple of 512.
965        */
966       rsize = (rsize/512) * 512;
967 #endif
968    }
969
970    /** a RAW device read on win32 only works if the buffer is a multiple of 512 */
971 #ifdef HAVE_WIN32
972    if (S_ISBLK(ff_pkt->statp.st_mode))
973       rsize = (rsize/512) * 512;
974 #endif
975    
976    /**
977     * Read the file data
978     */
979    while ((sd->msglen=(uint32_t)bread(&ff_pkt->bfd, rbuf, rsize)) > 0) {
980
981       /** Check for sparse blocks */
982       if (ff_pkt->flags & FO_SPARSE) {
983          ser_declare;
984          bool allZeros = false;
985          if ((sd->msglen == rsize &&
986               fileAddr+sd->msglen < (uint64_t)ff_pkt->statp.st_size) ||
987              ((ff_pkt->type == FT_RAW || ff_pkt->type == FT_FIFO) &&
988                (uint64_t)ff_pkt->statp.st_size == 0)) {
989             allZeros = is_buf_zero(rbuf, rsize);
990          }
991          if (!allZeros) {
992             /** Put file address as first data in buffer */
993             ser_begin(wbuf, OFFSET_FADDR_SIZE);
994             ser_uint64(fileAddr);     /* store fileAddr in begin of buffer */
995          }
996          fileAddr += sd->msglen;      /* update file address */
997          /** Skip block of all zeros */
998          if (allZeros) {
999             continue;                 /* skip block of zeros */
1000          }
1001       } else if (ff_pkt->flags & FO_OFFSETS) {
1002          ser_declare;
1003          ser_begin(wbuf, OFFSET_FADDR_SIZE);
1004          ser_uint64(ff_pkt->bfd.offset);     /* store offset in begin of buffer */
1005       }
1006
1007       jcr->ReadBytes += sd->msglen;         /* count bytes read */
1008
1009       /** Uncompressed cipher input length */
1010       cipher_input_len = sd->msglen;
1011
1012       /** Update checksum if requested */
1013       if (digest) {
1014          crypto_digest_update(digest, (uint8_t *)rbuf, sd->msglen);
1015       }
1016
1017       /** Update signing digest if requested */
1018       if (signing_digest) {
1019          crypto_digest_update(signing_digest, (uint8_t *)rbuf, sd->msglen);
1020       }
1021
1022 #ifdef HAVE_LIBZ
1023       /** Do compression if turned on */
1024       if (ff_pkt->flags & FO_COMPRESS && ff_pkt->Compress_algo == COMPRESS_GZIP && jcr->pZLIB_compress_workset) {
1025          Dmsg3(400, "cbuf=0x%x rbuf=0x%x len=%u\n", cbuf, rbuf, sd->msglen);
1026          
1027          ((z_stream*)jcr->pZLIB_compress_workset)->next_in   = (Bytef *)rbuf;
1028                 ((z_stream*)jcr->pZLIB_compress_workset)->avail_in  = sd->msglen;
1029          ((z_stream*)jcr->pZLIB_compress_workset)->next_out  = (Bytef *)cbuf;
1030                 ((z_stream*)jcr->pZLIB_compress_workset)->avail_out = max_compress_len;
1031
1032          if ((zstat=deflate((z_stream*)jcr->pZLIB_compress_workset, Z_FINISH)) != Z_STREAM_END) {
1033             Jmsg(jcr, M_FATAL, 0, _("Compression deflate error: %d\n"), zstat);
1034             jcr->setJobStatus(JS_ErrorTerminated);
1035             goto err;
1036          }
1037          compress_len = ((z_stream*)jcr->pZLIB_compress_workset)->total_out;
1038          /** reset zlib stream to be able to begin from scratch again */
1039          if ((zstat=deflateReset((z_stream*)jcr->pZLIB_compress_workset)) != Z_OK) {
1040             Jmsg(jcr, M_FATAL, 0, _("Compression deflateReset error: %d\n"), zstat);
1041             jcr->setJobStatus(JS_ErrorTerminated);
1042             goto err;
1043          }
1044
1045          Dmsg2(400, "GZIP compressed len=%d uncompressed len=%d\n", compress_len, 
1046                sd->msglen);
1047
1048          sd->msglen = compress_len;      /* set compressed length */
1049          cipher_input_len = compress_len;
1050       }
1051 #endif
1052 #ifdef HAVE_LZO
1053       /** Do compression if turned on */
1054       if (ff_pkt->flags & FO_COMPRESS && ff_pkt->Compress_algo == COMPRESS_LZO1X && jcr->LZO_compress_workset) {
1055          ser_declare;
1056          ser_begin(cbuf, sizeof(comp_stream_header));
1057
1058          Dmsg3(400, "cbuf=0x%x rbuf=0x%x len=%u\n", cbuf, rbuf, sd->msglen);
1059
1060          lzores = lzo1x_1_compress((const unsigned char*)rbuf, sd->msglen, cbuf2, &compress_len, jcr->LZO_compress_workset);
1061          if (lzores == LZO_E_OK && compress_len <= max_compress_len)
1062          {
1063             /* complete header */
1064             ser_uint32(COMPRESS_LZO1X);
1065             ser_uint32(compress_len);
1066             ser_uint16(ch.level);
1067             ser_uint16(ch.version);
1068          } else {
1069             /** this should NEVER happen */
1070             Jmsg(jcr, M_FATAL, 0, _("Compression LZO error: %d\n"), lzores);
1071             jcr->setJobStatus(JS_ErrorTerminated);
1072             goto err;
1073          }
1074
1075          Dmsg2(400, "LZO compressed len=%d uncompressed len=%d\n", compress_len, 
1076                sd->msglen);
1077
1078          compress_len += sizeof(comp_stream_header); /* add size of header */
1079          sd->msglen = compress_len;      /* set compressed length */
1080          cipher_input_len = compress_len;
1081       }
1082 #endif
1083
1084       /**
1085        * Note, here we prepend the current record length to the beginning
1086        *  of the encrypted data. This is because both sparse and compression
1087        *  restore handling want records returned to them with exactly the
1088        *  same number of bytes that were processed in the backup handling.
1089        *  That is, both are block filters rather than a stream.  When doing
1090        *  compression, the compression routines may buffer data, so that for
1091        *  any one record compressed, when it is decompressed the same size
1092        *  will not be obtained. Of course, the buffered data eventually comes
1093        *  out in subsequent crypto_cipher_update() calls or at least
1094        *  when crypto_cipher_finalize() is called.  Unfortunately, this
1095        *  "feature" of encryption enormously complicates the restore code.
1096        */
1097       if (ff_pkt->flags & FO_ENCRYPT) {
1098          uint32_t initial_len = 0;
1099          ser_declare;
1100
1101          if ((ff_pkt->flags & FO_SPARSE) || (ff_pkt->flags & FO_OFFSETS)) {
1102             cipher_input_len += OFFSET_FADDR_SIZE;
1103          }
1104
1105          /** Encrypt the length of the input block */
1106          uint8_t packet_len[sizeof(uint32_t)];
1107
1108          ser_begin(packet_len, sizeof(uint32_t));
1109          ser_uint32(cipher_input_len);    /* store data len in begin of buffer */
1110          Dmsg1(20, "Encrypt len=%d\n", cipher_input_len);
1111
1112          if (!crypto_cipher_update(cipher_ctx, packet_len, sizeof(packet_len),
1113              (uint8_t *)jcr->crypto.crypto_buf, &initial_len)) {
1114             /** Encryption failed. Shouldn't happen. */
1115             Jmsg(jcr, M_FATAL, 0, _("Encryption error\n"));
1116             goto err;
1117          }
1118
1119          /** Encrypt the input block */
1120          if (crypto_cipher_update(cipher_ctx, cipher_input, cipher_input_len, 
1121              (uint8_t *)&jcr->crypto.crypto_buf[initial_len], &encrypted_len)) {
1122             if ((initial_len + encrypted_len) == 0) {
1123                /** No full block of data available, read more data */
1124                continue;
1125             }
1126             Dmsg2(400, "encrypted len=%d unencrypted len=%d\n", encrypted_len, 
1127                   sd->msglen);
1128             sd->msglen = initial_len + encrypted_len; /* set encrypted length */
1129          } else {
1130             /** Encryption failed. Shouldn't happen. */
1131             Jmsg(jcr, M_FATAL, 0, _("Encryption error\n"));
1132             goto err;
1133          }
1134       }
1135
1136       /* Send the buffer to the Storage daemon */
1137       if ((ff_pkt->flags & FO_SPARSE) || (ff_pkt->flags & FO_OFFSETS)) {
1138          sd->msglen += OFFSET_FADDR_SIZE; /* include fileAddr in size */
1139       }
1140       sd->msg = wbuf;              /* set correct write buffer */
1141       if (!sd->send()) {
1142          if (!jcr->is_job_canceled()) {
1143             Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
1144                   sd->bstrerror());
1145          }
1146          goto err;
1147       }
1148       Dmsg1(130, "Send data to SD len=%d\n", sd->msglen);
1149       /*          #endif */
1150       jcr->JobBytes += sd->msglen;      /* count bytes saved possibly compressed/encrypted */
1151       sd->msg = msgsave;                /* restore read buffer */
1152
1153    } /* end while read file data */
1154
1155    if (sd->msglen < 0) {                 /* error */
1156       berrno be;
1157       Jmsg(jcr, M_ERROR, 0, _("Read error on file %s. ERR=%s\n"),
1158          ff_pkt->fname, be.bstrerror(ff_pkt->bfd.berrno));
1159       if (jcr->JobErrors++ > 1000) {       /* insanity check */
1160          Jmsg(jcr, M_FATAL, 0, _("Too many errors. JobErrors=%d.\n"), jcr->JobErrors);
1161       }
1162    } else if (ff_pkt->flags & FO_ENCRYPT) {
1163       /** 
1164        * For encryption, we must call finalize to push out any
1165        *  buffered data.
1166        */
1167       if (!crypto_cipher_finalize(cipher_ctx, (uint8_t *)jcr->crypto.crypto_buf, 
1168            &encrypted_len)) {
1169          /* Padding failed. Shouldn't happen. */
1170          Jmsg(jcr, M_FATAL, 0, _("Encryption padding error\n"));
1171          goto err;
1172       }
1173
1174       /** Note, on SSL pre-0.9.7, there is always some output */
1175       if (encrypted_len > 0) {
1176          sd->msglen = encrypted_len;      /* set encrypted length */
1177          sd->msg = jcr->crypto.crypto_buf;       /* set correct write buffer */
1178          if (!sd->send()) {
1179             if (!jcr->is_job_canceled()) {
1180                Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
1181                      sd->bstrerror());
1182             }
1183             goto err;
1184          }
1185          Dmsg1(130, "Send data to SD len=%d\n", sd->msglen);
1186          jcr->JobBytes += sd->msglen;     /* count bytes saved possibly compressed/encrypted */
1187          sd->msg = msgsave;               /* restore bnet buffer */
1188       }
1189    }
1190
1191    if (!sd->signal(BNET_EOD)) {        /* indicate end of file data */
1192       if (!jcr->is_job_canceled()) {
1193          Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
1194                sd->bstrerror());
1195       }
1196       goto err;
1197    }
1198
1199    /** Free the cipher context */
1200    if (cipher_ctx) {
1201       crypto_cipher_free(cipher_ctx);
1202    }
1203    return 1;
1204
1205 err:
1206    /** Free the cipher context */
1207    if (cipher_ctx) {
1208       crypto_cipher_free(cipher_ctx);
1209    }
1210
1211    sd->msg = msgsave; /* restore bnet buffer */
1212    sd->msglen = 0;
1213    return 0;
1214 }
1215
1216 bool encode_and_send_attributes(JCR *jcr, FF_PKT *ff_pkt, int &data_stream) 
1217 {
1218    BSOCK *sd = jcr->store_bsock;
1219    char attribs[MAXSTRING];
1220    char attribsExBuf[MAXSTRING];
1221    char *attribsEx = NULL;
1222    int attr_stream;
1223    int comp_len;
1224    bool stat;
1225    int hangup = get_hangup();
1226 #ifdef FD_NO_SEND_TEST
1227    return true;
1228 #endif
1229
1230    Dmsg1(300, "encode_and_send_attrs fname=%s\n", ff_pkt->fname);
1231    /** Find what data stream we will use, then encode the attributes */
1232    if ((data_stream = select_data_stream(ff_pkt)) == STREAM_NONE) {
1233       /* This should not happen */
1234       Jmsg0(jcr, M_FATAL, 0, _("Invalid file flags, no supported data stream type.\n"));
1235       return false;
1236    }
1237    encode_stat(attribs, &ff_pkt->statp, sizeof(ff_pkt->statp), ff_pkt->LinkFI, data_stream);
1238
1239    /** Now possibly extend the attributes */
1240    if (ff_pkt->type == FT_RESTORE_FIRST) {
1241       attr_stream = STREAM_RESTORE_OBJECT;
1242    } else {
1243       attribsEx = attribsExBuf;
1244       attr_stream = encode_attribsEx(jcr, attribsEx, ff_pkt);
1245    }
1246
1247    Dmsg3(300, "File %s\nattribs=%s\nattribsEx=%s\n", ff_pkt->fname, attribs, attribsEx);
1248
1249    jcr->lock();
1250    jcr->JobFiles++;                    /* increment number of files sent */
1251    ff_pkt->FileIndex = jcr->JobFiles;  /* return FileIndex */
1252    pm_strcpy(jcr->last_fname, ff_pkt->fname);
1253    jcr->unlock();
1254
1255    /* Debug code: check if we must hangup */
1256    if (hangup && (jcr->JobFiles > (uint32_t)hangup)) {
1257       jcr->setJobStatus(JS_Incomplete);
1258       Jmsg1(jcr, M_FATAL, 0, "Debug hangup requested after %d files.\n", hangup);
1259       set_hangup(0);
1260       return false;
1261    }
1262
1263    /**
1264     * Send Attributes header to Storage daemon
1265     *    <file-index> <stream> <info>
1266     */
1267    if (!sd->fsend("%ld %d 0", jcr->JobFiles, attr_stream)) {
1268       if (!jcr->is_canceled() && !jcr->is_incomplete()) {
1269          Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
1270                sd->bstrerror());
1271       }
1272       return false;
1273    }
1274    Dmsg1(300, ">stored: attrhdr %s\n", sd->msg);
1275
1276    /**
1277     * Send file attributes to Storage daemon
1278     *   File_index
1279     *   File type
1280     *   Filename (full path)
1281     *   Encoded attributes
1282     *   Link name (if type==FT_LNK or FT_LNKSAVED)
1283     *   Encoded extended-attributes (for Win32)
1284     *
1285     * or send Restore Object to Storage daemon
1286     *   File_index
1287     *   File_type
1288     *   Object_index
1289     *   Object_len  (possibly compressed)
1290     *   Object_full_len (not compressed)
1291     *   Object_compression
1292     *   Plugin_name
1293     *   Object_name
1294     *   Binary Object data
1295     *
1296     * For a directory, link is the same as fname, but with trailing
1297     * slash. For a linked file, link is the link.
1298     */
1299    if (ff_pkt->type != FT_DELETED) { /* already stripped */
1300       strip_path(ff_pkt);
1301    }
1302    switch (ff_pkt->type) {
1303    case FT_LNK:
1304    case FT_LNKSAVED:
1305       Dmsg3(300, "Link %d %s to %s\n", jcr->JobFiles, ff_pkt->fname, ff_pkt->link);
1306       stat = sd->fsend("%ld %d %s%c%s%c%s%c%s%c%u%c", jcr->JobFiles,
1307                        ff_pkt->type, ff_pkt->fname, 0, attribs, 0, 
1308                        ff_pkt->link, 0, attribsEx, 0, ff_pkt->delta_seq, 0);
1309       break;
1310    case FT_DIREND:
1311    case FT_REPARSE:
1312    case FT_JUNCTION:
1313       /* Here link is the canonical filename (i.e. with trailing slash) */
1314       stat = sd->fsend("%ld %d %s%c%s%c%c%s%c%u%c", jcr->JobFiles,
1315                        ff_pkt->type, ff_pkt->link, 0, attribs, 0, 0, 
1316                        attribsEx, 0, ff_pkt->delta_seq, 0);
1317       break;
1318    case FT_RESTORE_FIRST:
1319       comp_len = ff_pkt->object_len;
1320       ff_pkt->object_compression = 0;
1321       if (ff_pkt->object_len > 1000) {
1322          /* Big object, compress it */
1323          comp_len = ff_pkt->object_len + 1000;
1324          POOLMEM *comp_obj = get_memory(comp_len);
1325          stat = Zdeflate(ff_pkt->object, ff_pkt->object_len, comp_obj, comp_len);
1326          if (comp_len < ff_pkt->object_len) {
1327             ff_pkt->object = comp_obj;
1328             ff_pkt->object_compression = 1;    /* zlib level 9 compression */
1329          } else {
1330             /* Uncompressed object smaller, use it */
1331             comp_len = ff_pkt->object_len;
1332          }
1333          Dmsg2(100, "Object compressed from %d to %d bytes\n", ff_pkt->object_len, comp_len);
1334       }
1335       sd->msglen = Mmsg(sd->msg, "%d %d %d %d %d %d %s%c%s%c",
1336                         jcr->JobFiles, ff_pkt->type, ff_pkt->object_index,
1337                         comp_len, ff_pkt->object_len, ff_pkt->object_compression,
1338                         ff_pkt->fname, 0, ff_pkt->object_name, 0);
1339       sd->msg = check_pool_memory_size(sd->msg, sd->msglen + comp_len + 2);
1340       memcpy(sd->msg + sd->msglen, ff_pkt->object, comp_len);
1341       /* Note we send one extra byte so Dir can store zero after object */
1342       sd->msglen += comp_len + 1;
1343       stat = sd->send();
1344       if (ff_pkt->object_compression) {
1345          free_and_null_pool_memory(ff_pkt->object);
1346       }
1347       break;
1348    case FT_REG:
1349       stat = sd->fsend("%ld %d %s%c%s%c%c%s%c%d%c", jcr->JobFiles,
1350                ff_pkt->type, ff_pkt->fname, 0, attribs, 0, 0, attribsEx, 0,
1351                ff_pkt->delta_seq, 0);
1352       break;
1353    default:
1354       stat = sd->fsend("%ld %d %s%c%s%c%c%s%c%u%c", jcr->JobFiles,
1355                        ff_pkt->type, ff_pkt->fname, 0, attribs, 0, 0,
1356                        attribsEx, 0, ff_pkt->delta_seq, 0);
1357       break;
1358    }
1359    if (ff_pkt->type != FT_DELETED) {
1360       unstrip_path(ff_pkt);
1361    }
1362
1363    Dmsg2(300, ">stored: attr len=%d: %s\n", sd->msglen, sd->msg);
1364    if (!stat && !jcr->is_job_canceled()) {
1365       Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
1366             sd->bstrerror());
1367    }
1368    sd->signal(BNET_EOD);            /* indicate end of attributes data */
1369    return stat;
1370 }
1371
1372 /**
1373  * Do in place strip of path
1374  */
1375 static bool do_strip(int count, char *in)
1376 {
1377    char *out = in;
1378    int stripped;
1379    int numsep = 0;
1380
1381    /** Copy to first path separator -- Win32 might have c: ... */
1382    while (*in && !IsPathSeparator(*in)) {    
1383       out++; in++;
1384    }
1385    out++; in++;
1386    numsep++;                     /* one separator seen */
1387    for (stripped=0; stripped<count && *in; stripped++) {
1388       while (*in && !IsPathSeparator(*in)) {
1389          in++;                   /* skip chars */
1390       }
1391       if (*in) {
1392          numsep++;               /* count separators seen */
1393          in++;                   /* skip separator */
1394       }
1395    }
1396    /* Copy to end */
1397    while (*in) {                /* copy to end */
1398       if (IsPathSeparator(*in)) {
1399          numsep++;
1400       }
1401       *out++ = *in++;
1402    }
1403    *out = 0;
1404    Dmsg4(500, "stripped=%d count=%d numsep=%d sep>count=%d\n", 
1405          stripped, count, numsep, numsep>count);
1406    return stripped==count && numsep>count;
1407 }
1408
1409 /**
1410  * If requested strip leading components of the path so that we can
1411  *   save file as if it came from a subdirectory.  This is most useful
1412  *   for dealing with snapshots, by removing the snapshot directory, or
1413  *   in handling vendor migrations where files have been restored with
1414  *   a vendor product into a subdirectory.
1415  */
1416 void strip_path(FF_PKT *ff_pkt)
1417 {
1418    if (!(ff_pkt->flags & FO_STRIPPATH) || ff_pkt->strip_path <= 0) {
1419       Dmsg1(200, "No strip for %s\n", ff_pkt->fname);
1420       return;
1421    }
1422    if (!ff_pkt->fname_save) {
1423      ff_pkt->fname_save = get_pool_memory(PM_FNAME); 
1424      ff_pkt->link_save = get_pool_memory(PM_FNAME);
1425    }
1426    pm_strcpy(ff_pkt->fname_save, ff_pkt->fname);
1427    if (ff_pkt->type != FT_LNK && ff_pkt->fname != ff_pkt->link) {
1428       pm_strcpy(ff_pkt->link_save, ff_pkt->link);
1429       Dmsg2(500, "strcpy link_save=%d link=%d\n", strlen(ff_pkt->link_save),
1430          strlen(ff_pkt->link));
1431       Dsm_check(200);
1432    }
1433
1434    /**
1435     * Strip path.  If it doesn't succeed put it back.  If
1436     *  it does, and there is a different link string,
1437     *  attempt to strip the link. If it fails, back them
1438     *  both back.
1439     * Do not strip symlinks.
1440     * I.e. if either stripping fails don't strip anything.
1441     */
1442    if (!do_strip(ff_pkt->strip_path, ff_pkt->fname)) {
1443       unstrip_path(ff_pkt);
1444       goto rtn;
1445    } 
1446    /** Strip links but not symlinks */
1447    if (ff_pkt->type != FT_LNK && ff_pkt->fname != ff_pkt->link) {
1448       if (!do_strip(ff_pkt->strip_path, ff_pkt->link)) {
1449          unstrip_path(ff_pkt);
1450       }
1451    }
1452
1453 rtn:
1454    Dmsg3(100, "fname=%s stripped=%s link=%s\n", ff_pkt->fname_save, ff_pkt->fname, 
1455        ff_pkt->link);
1456 }
1457
1458 void unstrip_path(FF_PKT *ff_pkt)
1459 {
1460    if (!(ff_pkt->flags & FO_STRIPPATH) || ff_pkt->strip_path <= 0) {
1461       return;
1462    }
1463    strcpy(ff_pkt->fname, ff_pkt->fname_save);
1464    if (ff_pkt->type != FT_LNK && ff_pkt->fname != ff_pkt->link) {
1465       Dmsg2(500, "strcpy link=%s link_save=%s\n", ff_pkt->link,
1466           ff_pkt->link_save);
1467       strcpy(ff_pkt->link, ff_pkt->link_save);
1468       Dmsg2(500, "strcpy link=%d link_save=%d\n", strlen(ff_pkt->link),
1469           strlen(ff_pkt->link_save));
1470       Dsm_check(200);
1471    }
1472 }
1473
1474 static void close_vss_backup_session(JCR *jcr)
1475 {
1476 #if defined(WIN32_VSS)
1477    /* STOP VSS ON WIN32 */
1478    /* tell vss to close the backup session */
1479    if (jcr->VSS) {
1480       if (g_pVSSClient->CloseBackup()) {             
1481          /* inform user about writer states */
1482          for (int i=0; i<(int)g_pVSSClient->GetWriterCount(); i++) {
1483             int msg_type = M_INFO;
1484             if (g_pVSSClient->GetWriterState(i) < 1) {
1485                msg_type = M_WARNING;
1486                jcr->JobErrors++;
1487             }
1488             Jmsg(jcr, msg_type, 0, _("VSS Writer (BackupComplete): %s\n"), g_pVSSClient->GetWriterInfo(i));
1489          }
1490       }
1491       WCHAR *metadata = g_pVSSClient->GetMetadata();
1492       if (metadata) {
1493          FF_PKT *ff_pkt = jcr->ff;
1494          ff_pkt->fname = (char *)"job";
1495          ff_pkt->type = FT_RESTORE_FIRST;
1496          ff_pkt->LinkFI = 0;
1497          ff_pkt->object_name = (char *)"job_metadata.xml";
1498          ff_pkt->object = (char *)metadata;
1499          ff_pkt->object_len = (wcslen(metadata) + 1) * sizeof(WCHAR);
1500          ff_pkt->object_index = (int)time(NULL);
1501          save_file(jcr, ff_pkt, true);
1502      }
1503    }
1504 #endif
1505 }