LCOV - code coverage report
Current view: top level - src/client/protocol1 - restore.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 37 73 50.7 %
Date: 2019-04-28 12:12:12 Functions: 3 4 75.0 %

          Line data    Source code
       1             : #include "../../burp.h"
       2             : #include "../../action.h"
       3             : #include "../../alloc.h"
       4             : #include "../../asfd.h"
       5             : #include "../../async.h"
       6             : #include "../../attribs.h"
       7             : #include "../../bfile.h"
       8             : #include "../../cmd.h"
       9             : #include "../../cntr.h"
      10             : #include "../../fsops.h"
      11             : #include "../../handy.h"
      12             : #include "../../log.h"
      13             : #include "../../protocol1/msg.h"
      14             : #include "../extrameta.h"
      15             : #include "../restore.h"
      16             : #include "restore.h"
      17             : 
      18           4 : static int do_restore_file_or_get_meta(struct asfd *asfd, struct BFILE *bfd,
      19             :         struct sbuf *sb, const char *fname,
      20             :         char **metadata, size_t *metalen,
      21             :         struct cntr *cntr, const char *rpath,
      22             :         const char *encryption_password)
      23             : {
      24           4 :         int ret=-1;
      25           4 :         int enccompressed=0;
      26           4 :         uint64_t rcvdbytes=0;
      27           4 :         uint64_t sentbytes=0;
      28           4 :         const char *encpassword=NULL;
      29           4 :         int key_deriv=0;
      30             : 
      31           4 :         if(sbuf_is_encrypted(sb))
      32             :         {
      33           2 :                 encpassword=encryption_password;
      34           2 :                 if(sb->encryption==ENCRYPTION_KEY_DERIVED)
      35           0 :                         key_deriv=1;
      36             :         }
      37           4 :         enccompressed=dpth_protocol1_is_compressed(sb->compression,
      38           4 :                 sb->protocol1->datapth.buf);
      39             : /*
      40             :         printf("%s \n", fname);
      41             :         if(encpassword && !enccompressed)
      42             :                 printf("encrypted and not compressed\n");
      43             :         else if(!encpassword && enccompressed)
      44             :                 printf("not encrypted and compressed\n");
      45             :         else if(!encpassword && !enccompressed)
      46             :                 printf("not encrypted and not compressed\n");
      47             :         else if(encpassword && enccompressed)
      48             :                 printf("encrypted and compressed\n");
      49             : */
      50             : 
      51           4 :         if(metadata)
      52             :         {
      53           0 :                 ret=transfer_gzfile_inl(asfd,
      54             : #ifdef HAVE_WIN32
      55             :                         sb,
      56             : #endif
      57             :                         NULL,
      58             :                         &rcvdbytes, &sentbytes, encpassword,
      59             :                         enccompressed, cntr, metadata,
      60           0 :                         key_deriv, sb->protocol1->salt);
      61           0 :                 *metalen=sentbytes;
      62             :                 // skip setting cntr, as we do not actually
      63             :                 // restore until a bit later
      64             :         }
      65             :         else
      66             :         {
      67           4 :                 ret=transfer_gzfile_inl(asfd,
      68             : #ifdef HAVE_WIN32
      69             :                         sb,
      70             : #endif
      71             :                         bfd,
      72             :                         &rcvdbytes, &sentbytes,
      73             :                         encpassword, enccompressed,
      74           4 :                         cntr, NULL, key_deriv, sb->protocol1->salt);
      75             : #ifndef HAVE_WIN32
      76           4 :                 if(bfd && bfd->close(bfd, asfd))
      77             :                 {
      78           0 :                         logp("error closing %s in %s\n",
      79             :                                 fname, __func__);
      80           0 :                         ret=-1;
      81             :                 }
      82             : #endif
      83           4 :                 if(!ret) attribs_set(asfd, rpath,
      84             :                         &sb->statp, sb->winattr, cntr);
      85             :         }
      86           4 :         if(ret)
      87             :         {
      88           0 :                 char msg[256]="";
      89           0 :                 snprintf(msg, sizeof(msg),
      90             :                         "Could not transfer file in: %s", rpath);
      91           0 :                 return restore_interrupt(asfd, sb, msg, cntr, PROTO_1);
      92             :         }
      93             :         return 0;
      94             : }
      95             : 
      96           4 : static int restore_file_or_get_meta(struct asfd *asfd, struct BFILE *bfd,
      97             :         struct sbuf *sb, const char *fname, enum action act,
      98             :         char **metadata, size_t *metalen, int vss_restore,
      99             :         struct cntr *cntr, const char *encyption_password)
     100             : {
     101           4 :         int ret=0;
     102           4 :         char *rpath=NULL;
     103             : 
     104           4 :         if(act==ACTION_VERIFY)
     105             :         {
     106           0 :                 cntr_add(cntr, sb->path.cmd, 1);
     107           0 :                 goto end;
     108             :         }
     109             : 
     110           4 :         if(build_path(fname, "", &rpath, NULL))
     111             :         {
     112           0 :                 char msg[256]="";
     113             :                 // failed - do a warning
     114           0 :                 snprintf(msg, sizeof(msg), "build path failed: %s", fname);
     115           0 :                 if(restore_interrupt(asfd, sb, msg, cntr, PROTO_1))
     116           0 :                         ret=-1;
     117             :                 goto end;
     118             :         }
     119             : 
     120             : #ifndef HAVE_WIN32
     121             :         // We always want to open the file if it is on Windows. Otherwise,
     122             :         // only open it if we are not doing metadata.
     123           4 :         if(!metadata)
     124             :         {
     125             : #endif
     126           4 :                 switch(open_for_restore(asfd,
     127             :                         bfd, rpath, sb, vss_restore, cntr, PROTO_1))
     128             :                 {
     129             :                         case OFR_OK: break;
     130             :                         case OFR_CONTINUE: goto end;
     131           0 :                         default: ret=-1; goto end;
     132             :                 }
     133             : #ifndef HAVE_WIN32
     134             :         }
     135             : #endif
     136             : 
     137           4 :         if(!(ret=do_restore_file_or_get_meta(asfd, bfd, sb, fname,
     138             :                 metadata, metalen, cntr, rpath, encyption_password)))
     139             :         {
     140             :                 // Only add to counters if we are not doing metadata. The
     141             :                 // actual metadata restore comes a bit later.
     142           4 :                 if(!metadata)
     143           4 :                         cntr_add(cntr, sb->path.cmd, 1);
     144             :         }
     145             : end:
     146           4 :         free_w(&rpath);
     147           4 :         if(ret) logp("restore_file error\n");
     148           4 :         return ret;
     149             : }
     150             : 
     151           0 : static int restore_metadata(struct asfd *asfd,
     152             :         struct BFILE *bfd, struct sbuf *sb,
     153             :         const char *fname, enum action act,
     154             :         int vss_restore, struct cntr *cntr, const char *encryption_password)
     155             : {
     156           0 :         int ret=-1;
     157           0 :         size_t metalen=0;
     158           0 :         char *metadata=NULL;
     159             : 
     160             :         // If it is directory metadata, try to make sure the directory
     161             :         // exists. Pass in NULL as the cntr, so no counting is done.
     162             :         // The actual directory entry will be coming after the metadata,
     163             :         // annoyingly. This is because of the way that the server is queuing
     164             :         // up directories to send after file data, so that the stat info on
     165             :         // them gets set correctly.
     166           0 :         if(act==ACTION_VERIFY)
     167             :         {
     168           0 :                 cntr_add(cntr, sb->path.cmd, 1);
     169           0 :                 ret=0;
     170           0 :                 goto end;
     171             :         }
     172             : 
     173             :         // Create the directory, but do not add to the counts.
     174           0 :         if(S_ISDIR(sb->statp.st_mode)
     175           0 :           && restore_dir(asfd, sb, fname, act, /*cntr*/NULL, PROTO_1))
     176             :                 goto end;
     177             : 
     178             :         // Read in the metadata...
     179           0 :         if(restore_file_or_get_meta(asfd, bfd, sb, fname, act,
     180             :                 &metadata, &metalen, vss_restore, cntr, encryption_password))
     181             :                         goto end;
     182           0 :         if(metadata)
     183             :         {
     184           0 :                 if(!set_extrameta(asfd,
     185             : #ifdef HAVE_WIN32
     186             :                         bfd,
     187             : #endif
     188             :                         fname,
     189             :                         metadata, metalen, cntr))
     190             :                 {
     191             : #ifndef HAVE_WIN32
     192             :                         // Set file times again, since we just diddled with the
     193             :                         // file. Do not set all attributes, as it will wipe
     194             :                         // out any security attributes (eg getcap /usr/bin/ping)
     195           0 :                         if(attribs_set_file_times(asfd, fname,
     196             :                                 &sb->statp, cntr))
     197             :                                         return -1;
     198           0 :                         cntr_add(cntr, sb->path.cmd, 1);
     199             : #endif
     200             :                 }
     201             :                 // Carry on if we could not set_extrameta.
     202             :         }
     203             :         ret=0;
     204             : end:
     205           0 :         free_w(&metadata);
     206           0 :         return ret;
     207             : }
     208             : 
     209           5 : int restore_switch_protocol1(struct asfd *asfd, struct sbuf *sb,
     210             :         const char *fullpath, enum action act,
     211             :         struct BFILE *bfd, int vss_restore, struct cntr *cntr,
     212             :         const char *encryption_password)
     213             : {
     214           5 :         switch(sb->path.cmd)
     215             :         {
     216             :                 case CMD_FILE:
     217             :                 case CMD_VSS_T:
     218             :                 case CMD_ENC_FILE:
     219             :                 case CMD_ENC_VSS_T:
     220             :                 case CMD_EFS_FILE:
     221           5 :                         if(!sb->protocol1->datapth.buf)
     222             :                         {
     223             :                                 char msg[256];
     224           1 :                                 snprintf(msg, sizeof(msg),
     225             :                                   "datapth not supplied for %s in %s\n",
     226             :                                         iobuf_to_printable(&sb->path),
     227             :                                         __func__);
     228           1 :                                 log_and_send(asfd, msg);
     229             :                                 return -1;
     230             :                         }
     231           4 :                         return restore_file_or_get_meta(asfd, bfd, sb,
     232             :                                 fullpath, act,
     233             :                                 NULL, NULL, vss_restore, cntr,
     234             :                                 encryption_password);
     235             :                 case CMD_METADATA:
     236             :                 case CMD_VSS:
     237             :                 case CMD_ENC_METADATA:
     238             :                 case CMD_ENC_VSS:
     239           0 :                         return restore_metadata(asfd, bfd, sb,
     240             :                                 fullpath, act,
     241             :                                 vss_restore, cntr, encryption_password);
     242             :                 default:
     243             :                         // Other cases (dir/links/etc) are handled in the
     244             :                         // calling function.
     245           0 :                         logp("unknown cmd: %s\n",
     246             :                                 iobuf_to_printable(&sb->path));
     247           0 :                         return -1;
     248             :         }
     249             : }

Generated by: LCOV version 1.13