LCOV - code coverage report
Current view: top level - src/server - backup.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 56 155 36.1 %
Date: 2017-05-01 Functions: 6 9 66.7 %

          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 "../conf.h"
       7             : #include "../cmd.h"
       8             : #include "../cntr.h"
       9             : #include "../fsops.h"
      10             : #include "../handy.h"
      11             : #include "../iobuf.h"
      12             : #include "../log.h"
      13             : #include "../run_script.h"
      14             : #include "auth.h"
      15             : #include "backup_phase1.h"
      16             : #include "backup_phase3.h"
      17             : #include "compress.h"
      18             : #include "delete.h"
      19             : #include "sdirs.h"
      20             : #include "protocol1/backup_phase2.h"
      21             : #include "protocol1/backup_phase4.h"
      22             : #include "protocol2/backup_phase2.h"
      23             : #include "protocol2/backup_phase4.h"
      24             : #include "backup.h"
      25             : 
      26           1 : static int open_log(struct asfd *asfd,
      27             :         struct sdirs *sdirs, struct conf **cconfs)
      28             : {
      29           1 :         int ret=-1;
      30           1 :         char *logpath=NULL;
      31           1 :         const char *peer_version=get_string(cconfs[OPT_PEER_VERSION]);
      32             : 
      33           1 :         if(!(logpath=prepend_s(sdirs->rworking, "log"))) goto end;
      34           1 :         if(log_fzp_set(logpath, cconfs))
      35             :         {
      36           0 :                 logp("could not open log file: %s\n", logpath);
      37             :                 goto end;
      38             :         }
      39             : 
      40           1 :         logp("Client version: %s\n", peer_version?:"");
      41           1 :         logp("Protocol: %d\n", (int)get_protocol(cconfs));
      42           1 :         if(get_int(cconfs[OPT_CLIENT_IS_WINDOWS]))
      43           0 :                 logp("Client is Windows\n");
      44             : 
      45             :         // Make sure a warning appears in the backup log.
      46             :         // The client will already have been sent a message with logw.
      47             :         // This time, prevent it sending a logw to the client by specifying
      48             :         // NULL for cntr.
      49           1 :         if(get_int(cconfs[OPT_VERSION_WARN])) version_warn(asfd, NULL, cconfs);
      50             : 
      51             :         ret=0;
      52             : end:
      53           1 :         free_w(&logpath);
      54           1 :         return ret;
      55             : }
      56             : 
      57           1 : static int write_incexc(const char *realworking, const char *incexc)
      58             : {
      59           1 :         int ret=-1;
      60           1 :         char *tmp=NULL;
      61           1 :         char *path=NULL;
      62           1 :         struct fzp *fzp=NULL;
      63             : 
      64           1 :         if(!incexc || !*incexc) return 0;
      65             : 
      66           0 :         if(!(path=prepend_s(realworking, "incexc"))
      67           0 :           || !(tmp=prepend(path, ".tmp"))
      68           0 :           || !(fzp=fzp_open(tmp, "wb")))
      69             :                 goto end;
      70             : 
      71           0 :         fzp_printf(fzp, "%s", incexc);
      72           0 :         if(fzp_close(&fzp))
      73             :         {
      74           0 :                 logp("error writing to %s in %s\n", tmp, __func__);
      75           0 :                 goto end;
      76             :         }
      77           0 :         if(do_rename(tmp, path))
      78             :                 goto end;
      79           0 :         ret=0;
      80             : end:
      81           0 :         free_w(&path);
      82           0 :         free_w(&tmp);
      83           0 :         return ret;
      84             : }
      85             : 
      86           1 : static int backup_phase1_server(struct async *as,
      87             :         struct sdirs *sdirs, struct conf **cconfs)
      88             : {
      89           1 :         int breaking=get_int(cconfs[OPT_BREAKPOINT]);
      90           1 :         if(breaking==1)
      91           0 :                 return breakpoint(breaking, __func__);
      92           1 :         return backup_phase1_server_all(as, sdirs, cconfs);
      93             : }
      94             : 
      95           0 : static int backup_phase2_server(struct async *as, struct sdirs *sdirs,
      96             :         const char *incexc, int resume, struct conf **cconfs)
      97             : {
      98           0 :         int breaking=get_int(cconfs[OPT_BREAKPOINT]);
      99           0 :         if(breaking==2)
     100           0 :                 return breakpoint(breaking, __func__);
     101             : 
     102           0 :         switch(get_protocol(cconfs))
     103             :         {
     104             :                 case PROTO_1:
     105           0 :                         return backup_phase2_server_protocol1(as, sdirs,
     106             :                                 incexc, resume, cconfs);
     107             :                 default:
     108           0 :                         return backup_phase2_server_protocol2(as, sdirs,
     109             :                                 resume, cconfs);
     110             :         }
     111             : }
     112             : 
     113           0 : static int backup_phase3_server(struct sdirs *sdirs, struct conf **cconfs)
     114             : {
     115           0 :         int breaking=get_int(cconfs[OPT_BREAKPOINT]);
     116           0 :         if(breaking==3)
     117           0 :                 return breakpoint(breaking, __func__);
     118             : 
     119           0 :         return backup_phase3_server_all(sdirs, cconfs);
     120             : }
     121             : 
     122           0 : static int backup_phase4_server(struct sdirs *sdirs, struct conf **cconfs)
     123             : {
     124           0 :         int breaking=get_int(cconfs[OPT_BREAKPOINT]);
     125           0 :         if(breaking==4)
     126           0 :                 return breakpoint(breaking, __func__);
     127             : 
     128           0 :         log_fzp_set(NULL, cconfs);
     129             :         // Phase4 will open logfp again (in case it is resuming).
     130           0 :         switch(get_protocol(cconfs))
     131             :         {
     132             :                 case PROTO_1:
     133           0 :                         return backup_phase4_server_protocol1(sdirs, cconfs);
     134             :                 default:
     135           0 :                         return backup_phase4_server_protocol2(sdirs, cconfs);
     136             :         }
     137             : }
     138             : 
     139           1 : static void log_rshash(struct conf **confs)
     140             : {
     141           2 :         if(get_protocol(confs)!=PROTO_1) return;
     142           0 :         logp("Using librsync hash %s\n",
     143             :                 rshash_to_str(get_e_rshash(confs[OPT_RSHASH])));
     144             : }
     145             : 
     146           1 : static int do_backup_server(struct async *as, struct sdirs *sdirs,
     147             :         struct conf **cconfs, const char *incexc, int resume)
     148             : {
     149           1 :         int ret=0;
     150           1 :         int do_phase2=1;
     151           1 :         struct asfd *asfd=as->asfd;
     152           1 :         enum protocol protocol=get_protocol(cconfs);
     153           1 :         struct cntr *cntr=get_cntr(cconfs);
     154             : 
     155           1 :         logp("in do_backup_server\n");
     156             : 
     157           1 :         log_rshash(cconfs);
     158             : 
     159           1 :         if(resume)
     160             :         {
     161           0 :                 if(sdirs_get_real_working_from_symlink(sdirs)
     162           0 :                   || sdirs_get_real_manifest(sdirs, protocol)
     163           0 :                   || open_log(asfd, sdirs, cconfs))
     164             :                         goto error;
     165             :         }
     166             :         else
     167             :         {
     168             :                 // Not resuming - need to set everything up fresh.
     169           1 :                 if(sdirs_create_real_working(sdirs,
     170           1 :                         get_string(cconfs[OPT_TIMESTAMP_FORMAT]))
     171           1 :                   || sdirs_get_real_manifest(sdirs, protocol)
     172           1 :                   || open_log(asfd, sdirs, cconfs))
     173             :                         goto error;
     174             : 
     175           1 :                 if(write_incexc(sdirs->rworking, incexc))
     176             :                 {
     177           0 :                         logp("unable to write incexc\n");
     178           0 :                         goto error;
     179             :                 }
     180             : 
     181           1 :                 if(backup_phase1_server(as, sdirs, cconfs))
     182             :                 {
     183           1 :                         logp("error in phase 1\n");
     184           1 :                         goto error;
     185             :                 }
     186             :         }
     187             : 
     188           0 :         if(resume)
     189             :         {
     190             :                 struct stat statp;
     191           0 :                 if(lstat(sdirs->phase1data, &statp)
     192           0 :                   && !lstat(sdirs->changed, &statp)
     193           0 :                   && !lstat(sdirs->unchanged, &statp))
     194             :                 {
     195             :                         // In this condition, it looks like there was an
     196             :                         // interruption during phase3. Skip phase2.
     197           0 :                         do_phase2=0;
     198             :                 }
     199             :         }
     200             : 
     201           0 :         if(do_phase2)
     202             :         {
     203           0 :                 if(backup_phase2_server(as, sdirs, incexc, resume, cconfs))
     204             :                 {
     205           0 :                         logp("error in backup phase 2\n");
     206           0 :                         goto error;
     207             :                 }
     208             : 
     209           0 :                 asfd->write_str(asfd, CMD_GEN, "okbackupend");
     210             :         }
     211             : 
     212             :         // Close the connection with the client, the rest of the job we can do
     213             :         // by ourselves.
     214           0 :         logp("Backup ending - disconnect from client.\n");
     215           0 :         if(asfd_flush_asio(asfd)) goto end;
     216           0 :         as->asfd_remove(as, asfd);
     217           0 :         asfd_close(asfd);
     218             : 
     219           0 :         if(backup_phase3_server(sdirs, cconfs))
     220             :         {
     221           0 :                 logp("error in backup phase 3\n");
     222           0 :                 goto error;
     223             :         }
     224             : 
     225           0 :         if(do_rename(sdirs->working, sdirs->finishing))
     226             :                 goto error;
     227             : 
     228           0 :         if(backup_phase4_server(sdirs, cconfs))
     229             :         {
     230           0 :                 logp("error in backup phase 4\n");
     231           0 :                 goto error;
     232             :         }
     233             : 
     234           0 :         cntr_print(cntr, ACTION_BACKUP, asfd);
     235           0 :         cntr_stats_to_file(cntr, sdirs->rworking, ACTION_BACKUP);
     236             : 
     237           0 :         if(protocol==PROTO_2)
     238             :         {
     239             :                 // Regenerate dindex before the symlink is renamed, so that the
     240             :                 // champ chooser cleanup does not try to remove data files
     241             :                 // whilst the dindex regeneration is happening.
     242           0 :                 if(regenerate_client_dindex(sdirs))
     243             :                         goto error;
     244             :         }
     245             : 
     246             :         // Move the symlink to indicate that we are now in the end phase. The
     247             :         // rename() race condition is automatically recoverable here.
     248           0 :         if(do_rename(sdirs->finishing, sdirs->current))
     249             :                 goto error;
     250             : 
     251           0 :         logp("Backup completed.\n");
     252           0 :         log_fzp_set(NULL, cconfs);
     253           0 :         compress_filename(sdirs->rworking,
     254             :                 "log", "log.gz", get_int(cconfs[OPT_COMPRESSION]));
     255             : 
     256           0 :         goto end;
     257             : error:
     258             :         ret=-1;
     259             : end:
     260             : 
     261           1 :         log_fzp_set(NULL, cconfs);
     262           1 :         return ret;
     263             : }
     264             : 
     265           1 : int run_backup(struct async *as, struct sdirs *sdirs, struct conf **cconfs,
     266             :         const char *incexc, int *timer_ret, int resume)
     267             : {
     268             :         int ret;
     269           1 :         char okstr[32]="";
     270           1 :         struct asfd *asfd=as->asfd;
     271           1 :         struct iobuf *rbuf=asfd->rbuf;
     272           1 :         const char *cname=get_string(cconfs[OPT_CNAME]);
     273             : 
     274           1 :         if(get_string(cconfs[OPT_RESTORE_CLIENT]))
     275             :         {
     276             :                 // This client is not the original client, so a backup might
     277             :                 // cause all sorts of trouble.
     278           0 :                 logp("Not allowing backup of %s\n", cname);
     279           0 :                 return asfd->write_str(asfd, CMD_GEN, "Backup is not allowed");
     280             :         }
     281             : 
     282             :         // Set quality of service bits on backups.
     283           1 :         asfd->set_bulk_packets(asfd);
     284             : 
     285           1 :         if(!strncmp_w(rbuf->buf, "backupphase1timed"))
     286             :         {
     287           0 :                 int a=0;
     288             :                 const char *args[12];
     289           0 :                 int checkonly=!strncmp_w(rbuf->buf, "backupphase1timedcheck");
     290           0 :                 if(checkonly) logp("Client asked for a timer check only.\n");
     291             : 
     292           0 :                 args[a++]=get_string(cconfs[OPT_TIMER_SCRIPT]);
     293           0 :                 args[a++]=cname;
     294           0 :                 args[a++]=sdirs->current;
     295           0 :                 args[a++]=sdirs->clients;
     296           0 :                 args[a++]="reserved1";
     297           0 :                 args[a++]="reserved2";
     298           0 :                 args[a++]=NULL;
     299           0 :                 if((*timer_ret=run_script(asfd, args,
     300             :                   get_strlist(cconfs[OPT_TIMER_ARG]),
     301             :                   cconfs,
     302             :                   1 /* wait */,
     303             :                   1 /* use logp */,
     304             :                   0 /* no log_remote */
     305             :                 ))<0)
     306             :                 {
     307           0 :                         logp("Error running timer script for %s\n",
     308             :                                 cname);
     309           0 :                         return *timer_ret;
     310             :                 }
     311           0 :                 if(*timer_ret)
     312             :                 {
     313           0 :                         if(!checkonly)
     314           0 :                                 logp("Not running backup of %s\n",
     315             :                                         cname);
     316           0 :                         return asfd->write_str(asfd,
     317             :                                 CMD_GEN, "timer conditions not met");
     318             :                 }
     319           0 :                 if(checkonly)
     320             :                 {
     321           0 :                         logp("Client asked for a timer check only,\n");
     322           0 :                         logp("so a backup is not happening right now.\n");
     323           0 :                         return asfd->write_str(asfd,
     324             :                                 CMD_GEN, "timer conditions met");
     325             :                 }
     326           0 :                 logp("Running backup of %s\n", cname);
     327             :         }
     328           1 :         else if(!get_int(cconfs[OPT_CLIENT_CAN_FORCE_BACKUP]))
     329             :         {
     330           0 :                 logp("Not allowing forced backup of %s\n", cname);
     331           0 :                 return asfd->write_str(asfd,
     332             :                         CMD_GEN, "Forced backup is not allowed");
     333             :         }
     334             : 
     335           1 :         snprintf(okstr, sizeof(okstr), "%s:%d",
     336             :                 resume?"resume":"ok", get_int(cconfs[OPT_COMPRESSION]));
     337           1 :         if(asfd->write_str(asfd, CMD_GEN, okstr)) return -1;
     338             : 
     339           1 :         if((ret=do_backup_server(as, sdirs, cconfs, incexc, resume)))
     340             :                 goto end;
     341             : 
     342           0 :         if((ret=delete_backups(sdirs, cname,
     343             :                 get_strlist(cconfs[OPT_KEEP]),
     344           0 :                 get_string(cconfs[OPT_MANUAL_DELETE]))))
     345             :                         goto end;
     346             : end:
     347           1 :         return ret;
     348             : }

Generated by: LCOV version 1.10