LCOV - code coverage report
Current view: top level - src/server - run_action.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 177 248 71.4 %
Date: 2019-04-28 12:12:12 Functions: 12 14 85.7 %

          Line data    Source code
       1             : #include "../burp.h"
       2             : #include "../action.h"
       3             : #include "../asfd.h"
       4             : #include "../async.h"
       5             : #include "../cmd.h"
       6             : #include "../cntr.h"
       7             : #include "../handy.h"
       8             : #include "../fsops.h"
       9             : #include "../iobuf.h"
      10             : #include "../lock.h"
      11             : #include "../log.h"
      12             : #include "../regexp.h"
      13             : #include "../run_script.h"
      14             : #include "backup.h"
      15             : #include "delete.h"
      16             : #include "diff.h"
      17             : #include "list.h"
      18             : #include "protocol2/restore.h"
      19             : #include "restore.h"
      20             : #include "rubble.h"
      21             : #include "sdirs.h"
      22             : #include "run_action.h"
      23             : #include "timestamp.h"
      24             : 
      25             : // FIX THIS: Somewhat haphazard.
      26             : /* Return 0 for everything OK. -1 for error, or 1 to mean that there was
      27             :    another process that has the lock. */
      28           2 : static int get_lock_sdirs_for_write(struct asfd *asfd, struct sdirs *sdirs)
      29             : {
      30             :         struct stat statp;
      31             : 
      32             :         // Make sure the lock directory exists.
      33           2 :         if(mkpath(&sdirs->lock_storage_for_write->path, sdirs->lockdir))
      34             :         {
      35           0 :                 asfd->write_str(asfd, CMD_ERROR, "problem with lock directory");
      36           0 :                 goto error;
      37             :         }
      38             : 
      39           2 :         lock_get(sdirs->lock_storage_for_write);
      40           2 :         switch(sdirs->lock_storage_for_write->status)
      41             :         {
      42             :                 case GET_LOCK_GOT: break;
      43             :                 case GET_LOCK_NOT_GOT:
      44           0 :                         if(!lstat(sdirs->finishing, &statp))
      45             :                         {
      46           0 :                                 char msg[256]="";
      47           0 :                                 logp("finalising previous backup\n");
      48           0 :                                 snprintf(msg, sizeof(msg),
      49             :                                         "Finalising previous backup of client. "
      50             :                                         "Please try again later.");
      51           0 :                                 asfd->write_str(asfd, CMD_ERROR, msg);
      52             :                         }
      53             :                         else
      54             :                         {
      55           0 :                                 logp("Another instance of client is already running.\n");
      56           0 :                                 asfd->write_str(asfd, CMD_ERROR,
      57             :                                         "another instance is already running");
      58             :                         }
      59             :                         goto lockedout;
      60             :                 case GET_LOCK_ERROR:
      61             :                 default:
      62           0 :                         logp("Problem with lock file on server: %s\n",
      63             :                                 sdirs->lock_storage_for_write->path);
      64           0 :                         asfd->write_str(asfd, CMD_ERROR,
      65             :                                 "problem with lock file on server");
      66           0 :                         goto error;
      67             :         }
      68             : 
      69             :         return 0;
      70             : lockedout:
      71             :         return 1;
      72             : error:
      73             :         return -1;
      74             : }
      75             : 
      76             : static int client_can_generic(struct conf **cconfs, enum conf_opt o)
      77             : {
      78           5 :         return get_int(cconfs[o]);
      79             : }
      80             : 
      81           0 : int client_can_monitor(struct conf **cconfs)
      82             : {
      83           0 :         return client_can_generic(cconfs, OPT_CLIENT_CAN_MONITOR);
      84             : }
      85             : 
      86           1 : static int client_can_restore(struct conf **cconfs)
      87             : {
      88           1 :         const char *restore_path=get_string(cconfs[OPT_RESTORE_PATH]);
      89             : 
      90             :         // If there is a restore file on the server, it is always OK.
      91           1 :         if(restore_path && is_reg_lstat(restore_path)==1)
      92             :         {
      93             :                 // Remove the file.
      94           0 :                 unlink(restore_path);
      95           0 :                 return 1;
      96             :         }
      97             : 
      98           1 :         return client_can_generic(cconfs, OPT_CLIENT_CAN_RESTORE);
      99             : }
     100             : 
     101           1 : static void maybe_do_notification(struct asfd *asfd,
     102             :         int status, const char *clientdir,
     103             :         const char *storagedir, const char *filename,
     104             :         const char *brv, struct conf **cconfs)
     105             : {
     106           1 :         int a=0;
     107             :         const char *args[12];
     108           1 :         struct cntr *cntr=get_cntr(cconfs);
     109           1 :         args[a++]=NULL; // Fill in the script name later.
     110           1 :         args[a++]=get_string(cconfs[OPT_CNAME]);
     111           1 :         args[a++]=clientdir;
     112           1 :         args[a++]=storagedir;
     113           1 :         args[a++]=filename;
     114           1 :         args[a++]=brv;
     115           1 :         if(status)
     116             :         {
     117           1 :                 args[0]=get_string(cconfs[OPT_N_FAILURE_SCRIPT]);
     118           1 :                 args[a++]="0";
     119           1 :                 args[a++]=NULL;
     120           1 :                 run_script(asfd, args, get_strlist(cconfs[OPT_N_FAILURE_ARG]),
     121             :                         cconfs, 1, 1, 1);
     122             :         }
     123           0 :         else if((get_int(cconfs[OPT_N_SUCCESS_WARNINGS_ONLY])
     124           0 :                 && cntr->ent[CMD_WARNING]->count > 0)
     125           0 :           || (get_int(cconfs[OPT_N_SUCCESS_CHANGES_ONLY])
     126           0 :                 && cntr->ent[CMD_TOTAL]->changed > 0)
     127           0 :           || (!get_int(cconfs[OPT_N_SUCCESS_WARNINGS_ONLY])
     128           0 :                 && !get_int(cconfs[OPT_N_SUCCESS_CHANGES_ONLY])))
     129             :         {
     130           0 :                 char warnings[32]="";
     131           0 :                 snprintf(warnings, sizeof(warnings), "%" PRIu64,
     132           0 :                         cntr->ent[CMD_WARNING]->count);
     133           0 :                 args[0]=get_string(cconfs[OPT_N_SUCCESS_SCRIPT]);
     134           0 :                 args[a++]=warnings;
     135           0 :                 args[a++]=NULL;
     136           0 :                 run_script(asfd, args, get_strlist(cconfs[OPT_N_SUCCESS_ARG]),
     137             :                         cconfs, 1, 1, 1);
     138             :         }
     139           1 : }
     140             : 
     141          12 : static int parse_restore_str(const char *str, enum action *act,
     142             :         char **backupnostr, char **restoreregex)
     143             : {
     144          12 :         int ret=-1;
     145          12 :         char *cp=NULL;
     146          12 :         char *copy=NULL;
     147             : 
     148          12 :         if(!str)
     149             :         {
     150           1 :                 logp("NULL passed to %s\n", __func__);
     151           1 :                 goto end;
     152             :         }
     153             : 
     154          11 :         if(!(copy=strdup_w(str, __func__)))
     155             :                 goto end;
     156             : 
     157          11 :         if(!strncmp_w(copy, "restore "))
     158           4 :                 *act=ACTION_RESTORE;
     159           7 :         else if(!strncmp_w(copy, "verify "))
     160           4 :                 *act=ACTION_VERIFY;
     161             :         else
     162             :         {
     163           3 :                 logp("Could not parse %s in %s\n", copy, __func__);
     164           3 :                 goto end;
     165             :         }
     166             : 
     167           8 :         if(!(cp=strchr(copy, ' ')))
     168             :         {
     169           0 :                 logp("Could not parse %s in %s\n", copy, __func__);
     170           0 :                 goto end;
     171             :         }
     172           8 :         cp++;
     173           8 :         if(!(*backupnostr=strdup_w(cp, __func__)))
     174             :                 goto end;
     175           8 :         if((cp=strchr(*backupnostr, ':')))
     176             :         {
     177           4 :                 *cp='\0';
     178           4 :                 cp++;
     179           4 :                 if(!(*restoreregex=strdup_w(cp, __func__)))
     180             :                         goto end;
     181             :         }
     182             : 
     183             :         ret=0;
     184             : end:
     185          12 :         free_w(&copy);
     186          12 :         return ret;
     187             : }
     188             : 
     189             : #ifndef UTEST
     190             : static
     191             : #endif
     192          12 : int parse_restore_str_and_set_confs(const char *str, enum action *act,
     193             :         struct conf **cconfs)
     194             : {
     195          12 :         int ret=-1;
     196          12 :         char *backupnostr=NULL;
     197          12 :         char *restoreregex=NULL;
     198             : 
     199          12 :         if(parse_restore_str(str, act, &backupnostr, &restoreregex))
     200             :                 goto end;
     201             : 
     202           8 :         if(set_string(cconfs[OPT_BACKUP], backupnostr))
     203             :                 goto end;
     204           8 :         if(restoreregex && *restoreregex
     205           2 :           && set_string(cconfs[OPT_REGEX], restoreregex))
     206             :                 goto end;
     207             :         ret=0;
     208             : end:
     209          12 :         free_w(&backupnostr);
     210          12 :         free_w(&restoreregex);
     211          12 :         return ret;
     212             : }
     213             : 
     214           2 : static int run_restore(struct asfd *asfd,
     215             :         struct sdirs *sdirs, struct conf **cconfs, int srestore)
     216             : {
     217           2 :         int ret=-1;
     218           2 :         char *dir_for_notify=NULL;
     219           2 :         enum action act=ACTION_RESTORE;
     220           2 :         struct iobuf *rbuf=asfd->rbuf;
     221           2 :         const char *cname=get_string(cconfs[OPT_CNAME]);
     222             : 
     223           2 :         if(parse_restore_str_and_set_confs(rbuf->buf, &act, cconfs))
     224             :                 goto end;
     225             : 
     226           2 :         iobuf_free_content(rbuf);
     227             : 
     228           2 :         if(act==ACTION_RESTORE)
     229             :         {
     230             :                 int r;
     231           1 :                 if((r=client_can_restore(cconfs))<0)
     232             :                         goto end;
     233           1 :                 else if(!r)
     234             :                 {
     235           0 :                         logp("Not allowing restore of %s\n", cname);
     236           0 :                         if(!asfd->write_str(asfd, CMD_GEN,
     237           0 :                                 "Client restore is not allowed")) ret=0;
     238             :                         goto end;
     239             :                 }
     240             :         }
     241           2 :         if(act==ACTION_VERIFY
     242           1 :           && !(client_can_generic(cconfs, OPT_CLIENT_CAN_VERIFY)))
     243             :         {
     244           0 :                 logp("Not allowing verify of %s\n", cname);
     245           0 :                 if(!asfd->write_str(asfd, CMD_GEN,
     246           0 :                         "Client verify is not allowed")) ret=0;
     247             :                 goto end;
     248             :         }
     249             : 
     250           2 :         if(asfd->write_str(asfd, CMD_GEN, "ok"))
     251             :                 goto end;
     252             : 
     253           2 :         ret=do_restore_server(asfd, sdirs, act,
     254             :                 srestore, &dir_for_notify, cconfs);
     255           2 :         if(dir_for_notify)
     256           0 :                 maybe_do_notification(asfd, ret,
     257           0 :                         sdirs->client, dir_for_notify,
     258             :                         act==ACTION_RESTORE?"restorelog":"verifylog",
     259           0 :                         act==ACTION_RESTORE?"restore":"verify",
     260             :                         cconfs);
     261             : end:
     262           2 :         free_w(&dir_for_notify);
     263           2 :         return ret;
     264             : }
     265             : 
     266           1 : static int run_delete(struct asfd *asfd,
     267             :         struct sdirs *sdirs, struct conf **cconfs)
     268             : {
     269           1 :         char *backupno=NULL;
     270           1 :         struct iobuf *rbuf=asfd->rbuf;
     271           1 :         const char *cname=get_string(cconfs[OPT_CNAME]);
     272           1 :         if(!client_can_generic(cconfs, OPT_CLIENT_CAN_DELETE))
     273             :         {
     274           0 :                 logp("Not allowing delete of %s\n", cname);
     275           0 :                 asfd->write_str(asfd, CMD_GEN, "Client delete is not allowed");
     276           0 :                 return -1;
     277             :         }
     278           1 :         backupno=rbuf->buf+strlen("delete ");
     279           1 :         return do_delete_server(asfd, sdirs,
     280             :                 get_cntr(cconfs), cname, backupno,
     281           1 :                 get_string(cconfs[OPT_MANUAL_DELETE]));
     282             : }
     283             : 
     284           1 : static int run_list(struct asfd *asfd,
     285             :         struct sdirs *sdirs, struct conf **cconfs)
     286             : {
     287           1 :         int ret=-1;
     288           1 :         char *cp=NULL;
     289           1 :         char *backupno=NULL;
     290           1 :         char *browsedir=NULL;
     291           1 :         char *listregex=NULL;
     292           1 :         struct iobuf *rbuf=asfd->rbuf;
     293             : 
     294           1 :         if(!client_can_generic(cconfs, OPT_CLIENT_CAN_LIST))
     295             :         {
     296           0 :                 logp("Not allowing list of %s\n",
     297             :                         get_string(cconfs[OPT_CNAME]));
     298           0 :                 asfd->write_str(asfd, CMD_GEN, "Client list is not allowed");
     299           0 :                 goto end;
     300             :         }
     301             : 
     302           1 :         if(!strncmp_w(rbuf->buf, "list "))
     303             :         {
     304           1 :                 if((cp=strchr(rbuf->buf, ':')))
     305             :                 {
     306           0 :                         *cp='\0';
     307           0 :                         if(!(listregex=strdup_w(cp+1, __func__)))
     308             :                                 goto end;
     309             :                 }
     310           1 :                 if(!(backupno=strdup_w(rbuf->buf+strlen("list "), __func__)))
     311             :                         goto end;
     312             :                 
     313             :         }
     314           0 :         else if(!strncmp_w(rbuf->buf, "listb "))
     315             :         {
     316           0 :                 if((cp=strchr(rbuf->buf, ':')))
     317             :                 {
     318           0 :                         *cp='\0';
     319           0 :                         if(!(browsedir=strdup_w(cp+1, __func__)))
     320             :                                 goto end;
     321             :                 }
     322           0 :                 strip_trailing_slashes(&browsedir);
     323           0 :                 if(!(backupno=strdup_w(rbuf->buf+strlen("listb "), __func__)))
     324             :                         goto end;
     325             :         }
     326           1 :         if(asfd->write_str(asfd, CMD_GEN, "ok")) goto end;
     327             : 
     328           1 :         iobuf_free_content(asfd->rbuf);
     329             : 
     330           1 :         if(list_server_init(asfd, sdirs, get_cntr(cconfs),
     331             :                 get_protocol(cconfs), backupno, listregex, browsedir))
     332             :                         goto end;
     333           1 :         ret=do_list_server();
     334             : end:
     335           1 :         free_w(&backupno);
     336           1 :         free_w(&browsedir);
     337           1 :         free_w(&listregex);
     338           1 :         list_server_free();
     339           1 :         return ret;
     340             : }
     341             : 
     342           1 : static int run_diff(struct asfd *asfd,
     343             :         struct sdirs *sdirs, struct conf **cconfs)
     344             : {
     345           1 :         int ret=-1;
     346           1 :         char *backup1=NULL;
     347           1 :         char *backup2=NULL;
     348           1 :         struct iobuf *rbuf=asfd->rbuf;
     349             : 
     350           1 :         if(!client_can_generic(cconfs, OPT_CLIENT_CAN_DIFF))
     351             :         {
     352           0 :                 logp("Not allowing diff of %s\n",
     353             :                         get_string(cconfs[OPT_CNAME]));
     354           0 :                 asfd->write_str(asfd, CMD_GEN, "Client diff is not allowed");
     355           0 :                 goto end;
     356             :         }
     357             : 
     358           1 :         if(!strncmp_w(rbuf->buf, "diff "))
     359             :         {
     360             :                 char *cp;
     361           1 :                 if((cp=strchr(rbuf->buf, ':')))
     362             :                 {
     363           0 :                         *cp='\0';
     364           0 :                         if(!(backup2=strdup_w(cp+1, __func__)))
     365             :                                 goto end;
     366             :                 }
     367           1 :                 if(!(backup1=strdup_w(rbuf->buf+strlen("diff "), __func__)))
     368             :                         goto end;
     369             :         }
     370           1 :         if(asfd->write_str(asfd, CMD_GEN, "ok")) goto end;
     371             : 
     372           1 :         iobuf_free_content(asfd->rbuf);
     373             : 
     374           1 :         ret=do_diff_server(asfd, sdirs,
     375             :                 get_cntr(cconfs), get_protocol(cconfs), backup1, backup2);
     376             : end:
     377           1 :         free_w(&backup1);
     378           1 :         free_w(&backup2);
     379           1 :         return ret;
     380             : }
     381             : 
     382             : static int unknown_command(struct asfd *asfd, const char *func)
     383             : {
     384           2 :         iobuf_log_unexpected(asfd->rbuf, func);
     385           2 :         asfd->write_str(asfd, CMD_ERROR, "unknown command");
     386             :         return -1;
     387             : }
     388             : 
     389           0 : static const char *buf_to_notify_str(struct iobuf *rbuf)
     390             : {
     391           0 :         const char *buf=rbuf->buf;
     392           0 :         if(!strncmp_w(buf, "backup")) return "backup";
     393           0 :         else if(!strncmp_w(buf, "restore")) return "restore";
     394           0 :         else if(!strncmp_w(buf, "verify")) return "verify";
     395           0 :         else if(!strncmp_w(buf, "delete")) return "delete";
     396           0 :         else if(!strncmp_w(buf, "list")) return "list";
     397             :         else return "unknown";
     398             : }
     399             : 
     400           9 : static int maybe_write_first_created_file(struct sdirs *sdirs)
     401             : {
     402           9 :         char tstmp[48]="";
     403           9 :         if(is_reg_lstat(sdirs->created)>0
     404           9 :           || is_lnk_lstat(sdirs->current)>0
     405           9 :           || is_lnk_lstat(sdirs->currenttmp)>0
     406           9 :           || is_lnk_lstat(sdirs->working)>0
     407           9 :           || is_lnk_lstat(sdirs->finishing)>0)
     408             :                 return 0;
     409             : 
     410           9 :         if(timestamp_get_new(/*index*/0,
     411             :                 tstmp, sizeof(tstmp),
     412             :                 /*bufforfile*/NULL, /*bs*/0,
     413             :                 /*format*/NULL))
     414             :                         return -1;
     415           9 :         return timestamp_write(sdirs->created, tstmp);
     416             : }
     417             : 
     418          10 : static int run_action_server_do(struct async *as, struct sdirs *sdirs,
     419             :         const char *incexc, int srestore, int *timer_ret, struct conf **cconfs)
     420             : {
     421             :         int ret;
     422          10 :         int resume=0;
     423          10 :         char msg[256]="";
     424          10 :         struct iobuf *rbuf=as->asfd->rbuf;
     425             : 
     426             :         // Make sure some directories exist.
     427          10 :         if(mkpath(&sdirs->current, sdirs->dedup))
     428             :         {
     429           1 :                 snprintf(msg, sizeof(msg),
     430             :                         "could not mkpath %s", sdirs->current);
     431           1 :                 log_and_send(as->asfd, msg);
     432           1 :                 return -1;
     433             :         }
     434             : 
     435           9 :         if(maybe_write_first_created_file(sdirs))
     436             :                 return -1;
     437             : 
     438           9 :         if(rbuf->cmd!=CMD_GEN)
     439           2 :                 return unknown_command(as->asfd, __func__);
     440             : 
     441             :         // List and diff should work well enough without needing to lock
     442             :         // anything.
     443           8 :         if(!strncmp_w(rbuf->buf, "list ")
     444           7 :           || !strncmp_w(rbuf->buf, "listb "))
     445           1 :                 return run_list(as->asfd, sdirs, cconfs);
     446             : 
     447           7 :         if(!strncmp_w(rbuf->buf, "diff "))
     448           1 :                 return run_diff(as->asfd, sdirs, cconfs);
     449             : 
     450             :         // Old clients will send 'delete', possibly accidentally due to the
     451             :         // user trying to use the new diff/long diff options.
     452             :         // Stop them from working, just to be safe.
     453           6 :         if(!strncmp_w(rbuf->buf, "delete "))
     454             :         {
     455           1 :                 logp("old style delete from %s denied\n",
     456             :                         get_string(cconfs[OPT_CNAME]));
     457           1 :                 as->asfd->write_str(as->asfd, CMD_ERROR,
     458             :                         "old style delete is not supported on this server");
     459           1 :                 return -1;
     460             :         }
     461             : 
     462             :         // Restore and verify should work well enough by locking only the
     463             :         // backup directory they are interested in.
     464           5 :         if(!strncmp_w(rbuf->buf, "restore ")
     465           4 :           || !strncmp_w(rbuf->buf, "verify "))
     466           2 :                 return run_restore(as->asfd, sdirs, cconfs, srestore);
     467             : 
     468           3 :         if(strncmp_w(rbuf->buf, "backup")
     469           2 :           && strncmp_w(rbuf->buf, "Delete "))
     470           2 :                 return unknown_command(as->asfd, __func__);
     471             : 
     472             :         // Beyond this point, only need to deal with backup and delete.
     473             :         // These require locking out all other backups and deletes.
     474             : 
     475           2 :         switch((ret=get_lock_sdirs_for_write(as->asfd, sdirs)))
     476             :         {
     477             :                 case 0: break; // OK.
     478             :                 case 1: return 1; // Locked out.
     479             :                 default: // Error.
     480           0 :                         maybe_do_notification(as->asfd, ret,
     481             :                                 "", "error in get_lock_sdirs()",
     482             :                                 "", buf_to_notify_str(rbuf), cconfs);
     483           0 :                         return -1;
     484             :         }
     485             : 
     486           2 :         switch((ret=check_for_rubble_and_clean(as, sdirs,
     487             :                 incexc, &resume, cconfs)))
     488             :         {
     489             :                 case 0: break; // OK.
     490             :                 case 1: return 1; // Now finalising.
     491             :                 default: // Error.
     492           0 :                         maybe_do_notification(as->asfd, ret,
     493             :                                 "", "error in check_for_rubble()",
     494             :                                 "", buf_to_notify_str(rbuf), cconfs);
     495           0 :                         return -1;
     496             :         }
     497             : 
     498           2 :         if(!strncmp_w(rbuf->buf, "Delete "))
     499           1 :                 return run_delete(as->asfd, sdirs, cconfs);
     500             : 
     501             :         // Only backup action left to deal with.
     502           1 :         ret=run_backup(as, sdirs,
     503             :                 cconfs, incexc, timer_ret, resume);
     504             : 
     505             :         // If this is a backup failure and the client has more servers
     506             :         // to failover to, do not notify.
     507           1 :         if(ret
     508           1 :           && get_int(cconfs[OPT_N_FAILURE_BACKUP_FAILOVERS_LEFT])
     509           1 :           && get_int(cconfs[OPT_BACKUP_FAILOVERS_LEFT]))
     510             :                 return ret;
     511             : 
     512           1 :         if(*timer_ret<0)
     513           0 :                 maybe_do_notification(as->asfd, ret,
     514             :                         "", "error running timer script",
     515             :                         "", "backup", cconfs);
     516           1 :         else if(!*timer_ret)
     517           1 :                 maybe_do_notification(as->asfd, ret,
     518           1 :                         sdirs->client, sdirs->current,
     519             :                         "log", "backup", cconfs);
     520             :         return ret;
     521             : }
     522             : 
     523          10 : int run_action_server(struct async *as,
     524             :         const char *incexc, int srestore, int *timer_ret, struct conf **cconfs)
     525             : {
     526          10 :         int ret=-1;
     527          10 :         struct sdirs *sdirs=NULL;
     528          10 :         if((sdirs=sdirs_alloc())
     529          10 :           && !sdirs_init_from_confs(sdirs, cconfs))
     530          10 :                 ret=run_action_server_do(as,
     531             :                         sdirs, incexc, srestore, timer_ret, cconfs);
     532          10 :         if(sdirs) lock_release(sdirs->lock_storage_for_write);
     533          10 :         sdirs_free(&sdirs);
     534          10 :         return ret;
     535             : }

Generated by: LCOV version 1.13