LCOV - code coverage report
Current view: top level - src/server - restore_sbuf.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 117 172 68.0 %
Date: 2022-08-30 22:36:43 Functions: 5 7 71.4 %

          Line data    Source code
       1             : #include "../burp.h"
       2             : #include "../alloc.h"
       3             : #include "../asfd.h"
       4             : #include "../async.h"
       5             : #include "../bu.h"
       6             : #include "../cmd.h"
       7             : #include "../cntr.h"
       8             : #include "../handy.h"
       9             : #include "../handy_extra.h"
      10             : #include "../hexmap.h"
      11             : #include "../log.h"
      12             : #include "../md5.h"
      13             : #include "../prepend.h"
      14             : #include "../server/backup_phase4.h"
      15             : #include "../server/link.h"
      16             : #include "../server/zlibio.h"
      17             : #include "../sbuf.h"
      18             : #include "../slist.h"
      19             : #include "dpth.h"
      20             : #include "sdirs.h"
      21             : #include "restore_sbuf.h"
      22             : 
      23             : #include <librsync.h>
      24             : 
      25           0 : static int create_zero_length_file(const char *path)
      26             : {
      27           0 :         int ret=0;
      28             :         struct fzp *dest;
      29           0 :         if(!(dest=fzp_open(path, "wb")))
      30           0 :                 ret=-1;
      31           0 :         ret|=fzp_close(&dest);
      32           0 :         return ret;
      33             : }
      34             : 
      35           0 : static int inflate_or_link_oldfile(struct asfd *asfd, const char *oldpath,
      36             :         const char *infpath, struct conf **cconfs, int compression)
      37             : {
      38           0 :         int ret=0;
      39             :         struct stat statp;
      40             : 
      41           0 :         if(lstat(oldpath, &statp))
      42             :         {
      43           0 :                 logp("could not lstat %s\n", oldpath);
      44           0 :                 return -1;
      45             :         }
      46             : 
      47           0 :         if(dpth_is_compressed(compression, oldpath))
      48             :         {
      49             :                 //logp("inflating...\n");
      50             : 
      51           0 :                 if(!statp.st_size)
      52             :                 {
      53             :                         // Empty file - cannot inflate.
      54           0 :                         logp("asked to inflate zero length file: %s\n",
      55             :                                 oldpath);
      56           0 :                         return create_zero_length_file(infpath);
      57             :                 }
      58             : 
      59           0 :                 if((ret=zlib_inflate(asfd, oldpath, infpath, get_cntr(cconfs))))
      60           0 :                         logp("zlib_inflate returned: %d\n", ret);
      61             :         }
      62             :         else
      63             :         {
      64             :                 // Not compressed - just hard link it.
      65           0 :                 if(do_link(oldpath, infpath, &statp, cconfs,
      66             :                         1 /* allow overwrite of infpath */))
      67             :                                 return -1;
      68             :         }
      69             :         return ret;
      70             : }
      71             : 
      72           4 : static int do_send_file(struct asfd *asfd, struct sbuf *sb,
      73             :         int patches, const char *best, struct cntr *cntr)
      74             : {
      75           4 :         enum send_e ret=SEND_FATAL;
      76             :         struct BFILE bfd;
      77           4 :         uint64_t bytes=0; // Unused.
      78             : 
      79           4 :         bfile_init(&bfd, 0, cntr);
      80           4 :         if(bfd.open_for_send(&bfd, asfd, best, sb->winattr,
      81             :                 1 /* no O_NOATIME */, cntr))
      82             :                         return SEND_FATAL;
      83           4 :         if(asfd->write(asfd, &sb->path))
      84             :                 ret=SEND_FATAL;
      85           4 :         else if(patches)
      86             :         {
      87             :                 // If we did some patches, the resulting file
      88             :                 // is not gzipped. Gzip it during the send.
      89           0 :                 ret=send_whole_file_gzl(
      90             :                         asfd,
      91           0 :                         sb->datapth.buf,
      92             :                         /*quick_read*/1,
      93             :                         &bytes,
      94             :                         /*encpassword*/NULL,
      95             :                         cntr,
      96             :                         /*compression*/9,
      97             :                         &bfd,
      98             :                         /*extrameta*/NULL,
      99             :                         /*elen*/0,
     100             :                         /*key_deriv*/ENCRYPTION_UNSET,
     101             :                         /*salt*/0
     102             :                 );
     103             :         }
     104             :         else
     105             :         {
     106             :                 // If it was encrypted, it may or may not have been compressed
     107             :                 // before encryption. Send it as it as, and let the client
     108             :                 // sort it out.
     109           4 :                 if(sbuf_is_encrypted(sb))
     110             :                 {
     111           2 :                         ret=send_whole_filel(asfd,
     112             : #ifdef HAVE_WIN32
     113             :                                 sb->path.cmd
     114             : #endif
     115           2 :                                 sb->datapth.buf,
     116             :                                 1, &bytes, cntr, &bfd, NULL, 0);
     117             :                 }
     118             :                 // It might have been stored uncompressed. Gzip it during
     119             :                 // the send. If the client knew what kind of file it would be
     120             :                 // receiving, this step could disappear.
     121           2 :                 else if(!dpth_is_compressed(sb->compression,
     122           2 :                         sb->datapth.buf))
     123             :                 {
     124           2 :                         ret=send_whole_file_gzl(
     125             :                                 asfd,
     126           2 :                                 sb->datapth.buf,
     127             :                                 /*quick_read*/1,
     128             :                                 &bytes,
     129             :                                 /*encpassword*/NULL,
     130             :                                 cntr,
     131             :                                 /*compression*/9,
     132             :                                 &bfd,
     133             :                                 /*extrameta*/NULL,
     134             :                                 /*elen*/0,
     135             :                                 /*key_deriv*/ENCRYPTION_UNSET,
     136             :                                 /*salt*/0
     137             :                         );
     138             :                 }
     139             :                 else
     140             :                 {
     141             :                         // If we did not do some patches, the resulting
     142             :                         // file might already be gzipped. Send it as it is.
     143           0 :                         ret=send_whole_filel(asfd,
     144             : #ifdef HAVE_WIN32
     145             :                                 sb->path.cmd
     146             : #endif
     147           0 :                                 sb->datapth.buf,
     148             :                                 1, &bytes, cntr, &bfd, NULL, 0);
     149             :                 }
     150             :         }
     151           4 :         bfd.close(&bfd, asfd);
     152             : 
     153           4 :         switch(ret)
     154             :         {
     155             :                 case SEND_OK:
     156             :                 case SEND_ERROR: // Carry on.
     157             :                         return 0;
     158             :                 case SEND_FATAL:
     159             :                 default:
     160           0 :                         return -1;
     161             :         }
     162             : }
     163             : 
     164             : #ifndef UTEST
     165             : static
     166             : #endif
     167           5 : int verify_file(struct asfd *asfd, struct sbuf *sb,
     168             :         int patches, const char *best, struct cntr *cntr)
     169             : {
     170           5 :         struct md5 *md5=NULL;
     171           5 :         int b=0;
     172           5 :         const char *cp=NULL;
     173           5 :         const char *newsum=NULL;
     174             :         uint8_t in[ZCHUNK];
     175             :         uint8_t checksum[MD5_DIGEST_LENGTH];
     176           5 :         uint64_t cbytes=0;
     177           5 :         struct fzp *fzp=NULL;
     178             : 
     179           5 :         if(!sb->endfile.buf
     180           5 :           || !(cp=strrchr(sb->endfile.buf, ':')))
     181             :         {
     182           1 :                 logw(asfd, cntr,
     183             :                         "%s has no md5sum!\n",
     184             :                         iobuf_to_printable(&sb->datapth));
     185           1 :                 return 0;
     186             :         }
     187           4 :         cp++;
     188           4 :         if(!(md5=md5_alloc(__func__)))
     189             :                 return -1;
     190           4 :         if(!md5_init(md5))
     191             :         {
     192           0 :                 logp("md5_init() failed\n");
     193           0 :                 md5_free(&md5);
     194           0 :                 return -1;
     195             :         }
     196           4 :         if(patches
     197           4 :           || sb->path.cmd==CMD_ENC_FILE
     198           4 :           || sb->path.cmd==CMD_ENC_METADATA
     199           4 :           || sb->path.cmd==CMD_EFS_FILE
     200           4 :           || sb->path.cmd==CMD_ENC_VSS
     201           4 :           || sb->path.cmd==CMD_ENC_VSS_T
     202           4 :           || (!patches && !dpth_is_compressed(sb->compression, best)))
     203           2 :                 fzp=fzp_open(best, "rb");
     204             :         else
     205           2 :                 fzp=fzp_gzopen(best, "rb");
     206             : 
     207           4 :         if(!fzp)
     208             :         {
     209           1 :                 logw(asfd, cntr, "could not open %s\n", best);
     210           1 :                 md5_free(&md5);
     211           1 :                 return 0;
     212             :         }
     213           5 :         while((b=fzp_read(fzp, in, ZCHUNK))>0)
     214             :         {
     215           2 :                 cbytes+=b;
     216           2 :                 if(!md5_update(md5, in, b))
     217             :                 {
     218           0 :                         logp("md5_update() failed\n");
     219           0 :                         fzp_close(&fzp);
     220           0 :                         md5_free(&md5);
     221           0 :                         return -1;
     222             :                 }
     223             :         }
     224           3 :         if(!fzp_eof(fzp))
     225             :         {
     226           1 :                 logw(asfd, cntr, "error while reading %s\n", best);
     227           1 :                 fzp_close(&fzp);
     228           1 :                 md5_free(&md5);
     229           1 :                 return 0;
     230             :         }
     231           2 :         fzp_close(&fzp);
     232           2 :         if(!md5_final(md5, checksum))
     233             :         {
     234           0 :                 logp("md5_final() failed\n");
     235           0 :                 md5_free(&md5);
     236           0 :                 return -1;
     237             :         }
     238           2 :         newsum=bytes_to_md5str(checksum);
     239           2 :         md5_free(&md5);
     240             : 
     241           2 :         if(strcmp(newsum, cp))
     242             :         {
     243           1 :                 logp("%s %s\n", newsum, cp);
     244           1 :                 logw(asfd, cntr, "md5sum for '%s (%s)' did not match!\n",
     245             :                         iobuf_to_printable(&sb->path),
     246             :                         iobuf_to_printable(&sb->datapth));
     247           1 :                 logp("md5sum for '%s (%s)' did not match!\n",
     248             :                         iobuf_to_printable(&sb->path),
     249             :                         iobuf_to_printable(&sb->datapth));
     250           1 :                 return 0;
     251             :         }
     252             : 
     253             :         // Just send the file name to the client, so that it can show cntr.
     254           1 :         if(asfd->write(asfd, &sb->path)) return -1;
     255           1 :         return 0;
     256             : }
     257             : 
     258           4 : static int process_data_dir_file(struct asfd *asfd,
     259             :         struct bu *bu, struct bu *b, const char *path,
     260             :         struct sbuf *sb, enum action act, struct sdirs *sdirs,
     261             :         struct conf **cconfs)
     262             : {
     263           4 :         int ret=-1;
     264           4 :         int patches=0;
     265           4 :         char *dpath=NULL;
     266             :         struct stat dstatp;
     267           4 :         const char *tmp=NULL;
     268           4 :         const char *best=NULL;
     269             :         static char *tmppath1=NULL;
     270             :         static char *tmppath2=NULL;
     271           4 :         struct cntr *cntr=NULL;
     272           4 :         if(cconfs) cntr=get_cntr(cconfs);
     273             : 
     274           4 :         if((!tmppath1 && !(tmppath1=prepend_s(bu->path, "tmp1")))
     275           4 :           || (!tmppath2 && !(tmppath2=prepend_s(bu->path, "tmp2"))))
     276             :                 goto end;
     277             : 
     278           4 :         best=path;
     279           4 :         tmp=tmppath1;
     280             :         // Now go down the list, applying any deltas.
     281           4 :         for(b=b->prev; b && b->next!=bu; b=b->prev)
     282             :         {
     283           0 :                 free_w(&dpath);
     284           0 :                 if(!(dpath=prepend_s(b->delta, sb->datapth.buf)))
     285             :                         goto end;
     286             : 
     287           0 :                 if(lstat(dpath, &dstatp) || !S_ISREG(dstatp.st_mode))
     288           0 :                         continue;
     289             : 
     290           0 :                 if(!patches)
     291             :                 {
     292             :                         // Need to gunzip the first one.
     293           0 :                         if(inflate_or_link_oldfile(asfd, best, tmp,
     294             :                                 cconfs, sb->compression))
     295             :                         {
     296           0 :                                 logw(asfd, cntr,
     297             :                                   "problem when inflating %s\n", best);
     298           0 :                                 ret=0;
     299             :                                 goto end;
     300             :                         }
     301           0 :                         best=tmp;
     302           0 :                         if(tmp==tmppath1) tmp=tmppath2;
     303             :                         else tmp=tmppath1;
     304             :                 }
     305             : 
     306           0 :                 if(do_patch(best, dpath, tmp,
     307             :                         0 /* do not gzip the result */,
     308             :                         sb->compression /* from the manifest */))
     309             :                 {
     310           0 :                         logw(asfd, cntr, "problem when patching %s with %s\n", path, b->timestamp);
     311           0 :                         ret=0;
     312             :                         goto end;
     313             :                 }
     314             : 
     315           0 :                 best=tmp;
     316           0 :                 if(tmp==tmppath1) tmp=tmppath2;
     317             :                 else tmp=tmppath1;
     318           0 :                 unlink(tmp);
     319           0 :                 patches++;
     320             :         }
     321             : 
     322           4 :         switch(act)
     323             :         {
     324             :                 case ACTION_RESTORE:
     325           4 :                         if(do_send_file(asfd, sb, patches, best, cntr))
     326             :                                 goto end;
     327             :                         break;
     328             :                 case ACTION_VERIFY:
     329           0 :                         if(verify_file(asfd, sb, patches, best, cntr))
     330             :                                 goto end;
     331             :                         break;
     332             :                 default:
     333           0 :                         logp("Unknown action: %d\n", act);
     334             :                         goto end;
     335             :         }
     336           4 :         cntr_add(cntr, sb->path.cmd, 0);
     337           4 :         cntr_add_bytes(cntr, strtoull(sb->endfile.buf, NULL, 10));
     338             : 
     339           4 :         ret=0;
     340             : end:
     341           4 :         free_w(&dpath);
     342           4 :         if(tmppath1) unlink(tmppath1);
     343           4 :         if(tmppath2) unlink(tmppath2);
     344           4 :         free_w(&tmppath1);
     345           4 :         free_w(&tmppath2);
     346           4 :         return ret;
     347             : }
     348             : 
     349             : // a = length of struct bu array
     350             : // i = position to restore from
     351             : #ifndef UTEST
     352             : static
     353             : #endif
     354           5 : int restore_file(struct asfd *asfd, struct bu *bu,
     355             :         struct sbuf *sb, enum action act,
     356             :         struct sdirs *sdirs, struct conf **cconfs)
     357             : {
     358           5 :         int ret=-1;
     359           5 :         char *path=NULL;
     360             :         struct bu *b;
     361           5 :         struct bu *hlwarn=NULL;
     362             :         struct stat statp;
     363           5 :         struct cntr *cntr=NULL;
     364           5 :         if(cconfs) cntr=get_cntr(cconfs);
     365             : 
     366             :         // Go up the array until we find the file in the data directory.
     367          10 :         for(b=bu; b; b=b->next)
     368             :         {
     369           4 :                 free_w(&path);
     370           4 :                 if(!(path=prepend_s(b->data, sb->datapth.buf)))
     371             :                         goto end;
     372             : 
     373           8 :                 if(lstat(path, &statp) || !S_ISREG(statp.st_mode))
     374           0 :                         continue;
     375             : 
     376           4 :                 if(b!=bu && (bu->flags & BU_HARDLINKED)) hlwarn=b;
     377             : 
     378           4 :                 if(process_data_dir_file(asfd, bu, b,
     379             :                         path, sb, act, sdirs, cconfs))
     380             :                                 goto end;
     381             : 
     382             :                 // This warning must be done after everything else,
     383             :                 // Because the client does not expect another cmd after
     384             :                 // the warning.
     385           4 :                 if(hlwarn) logw(asfd, cntr, "restore found %s in %s\n",
     386             :                         iobuf_to_printable(&sb->path),
     387             :                         hlwarn->basename);
     388             :                 ret=0; // All OK.
     389             :                 break;
     390             :         }
     391             : 
     392           5 :         if(!b)
     393             :         {
     394           1 :                 logw(asfd, cntr, "restore could not find %s (%s)\n",
     395             :                         iobuf_to_printable(&sb->path),
     396             :                         iobuf_to_printable(&sb->datapth));
     397           1 :                 ret=0; // Carry on to subsequent files.
     398             :         }
     399             : end:
     400           5 :         free_w(&path);
     401           5 :         return ret;
     402             : }
     403             : 
     404          10 : int restore_sbuf_all(struct asfd *asfd, struct sbuf *sb, struct bu *bu,
     405             :         enum action act, struct sdirs *sdirs, struct conf **cconfs)
     406             : {
     407          10 :         if((sb->datapth.buf
     408           4 :                 && asfd->write(asfd, &(sb->datapth)))
     409          10 :           || asfd->write(asfd, &sb->attr))
     410             :                 return -1;
     411          10 :         else if(sbuf_is_filedata(sb)
     412           6 :           || sbuf_is_vssdata(sb))
     413             :         {
     414           4 :                 if(!sb->datapth.buf)
     415             :                 {
     416           0 :                         logw(asfd, get_cntr(cconfs),
     417             :                                 "Got filedata entry with no datapth: %s\n",
     418             :                                         iobuf_to_printable(&sb->path));
     419           0 :                         return 0;
     420             :                 }
     421           4 :                 return restore_file(asfd, bu, sb, act, sdirs, cconfs);
     422             :         }
     423             :         else
     424             :         {
     425           6 :                 if(asfd->write(asfd, &sb->path))
     426             :                         return -1;
     427             :                 // If it is a link, send what
     428             :                 // it points to.
     429           6 :                 else if(sbuf_is_link(sb)
     430           4 :                   && asfd->write(asfd, &sb->link)) return -1;
     431           6 :                 cntr_add(get_cntr(cconfs), sb->path.cmd, 0);
     432             :         }
     433           6 :         return 0;
     434             : }

Generated by: LCOV version 1.13