LCOV - code coverage report
Current view: top level - src/server/monitor - cstat.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 127 128 99.2 %
Date: 2016-07-02 Functions: 9 9 100.0 %

          Line data    Source code
       1             : #include "../../burp.h"
       2             : #include "../../alloc.h"
       3             : #include "../../bu.h"
       4             : #include "../../cstat.h"
       5             : #include "../../conffile.h"
       6             : #include "../../fsops.h"
       7             : #include "../../lock.h"
       8             : #include "../../log.h"
       9             : #include "../../strlist.h"
      10             : #include "../bu_get.h"
      11             : #include "../sdirs.h"
      12             : #include "cstat.h"
      13             : 
      14             : #ifndef UTEST
      15             : static 
      16             : #endif
      17          19 : int cstat_permitted(struct cstat *cstat,
      18             :         struct conf **parentconfs, struct conf **cconfs)
      19             : {
      20             :         struct strlist *rclient;
      21             :         const char *parentconf_cname;
      22             : 
      23          19 :         parentconf_cname=get_string(parentconfs[OPT_CNAME]);
      24          19 :         if(!parentconf_cname) return 0;
      25             : 
      26             :         // Allow clients to look at themselves.
      27           4 :         if(!strcmp(cstat->name, parentconf_cname)) return 1;
      28             : 
      29             :         // Do not allow clients using the restore_client option to see more
      30             :         // than the client that it is pretending to be.
      31           3 :         if(get_string(parentconfs[OPT_RESTORE_CLIENT])) return 0;
      32             : 
      33             :         // If we are listed in this restore_client list.
      34           4 :         for(rclient=get_strlist(cconfs[OPT_RESTORE_CLIENTS]);
      35             :           rclient; rclient=rclient->next)
      36           3 :                 if(!strcmp(get_string(parentconfs[OPT_CNAME]), rclient->path))
      37             :                         return 1;
      38             :         return 0;
      39             : }
      40             : 
      41          15 : static int set_cstat_from_conf(struct cstat *cstat,
      42             :         struct conf **parentconfs, struct conf **cconfs)
      43             : {
      44          15 :         struct strlist *s=NULL;
      45             :         // Make sure the permitted flag is set appropriately.
      46          15 :         cstat->permitted=cstat_permitted(cstat, parentconfs, cconfs);
      47             : 
      48          15 :         cstat->protocol=get_protocol(cconfs);
      49          15 :         sdirs_free((struct sdirs **)&cstat->sdirs);
      50          30 :         if(!(cstat->sdirs=sdirs_alloc())
      51          15 :           || sdirs_init_from_confs((struct sdirs *)cstat->sdirs, cconfs))
      52             :                 return -1;
      53          15 :         strlists_free(&cstat->labels);
      54          21 :         for(s=get_strlist(cconfs[OPT_LABEL]); s; s=s->next)
      55           6 :                 if(strlist_add_sorted(&cstat->labels, s->path, s->flag))
      56             :                         return -1;
      57             :         return 0;
      58             : }
      59             : 
      60             : #ifndef UTEST
      61             : static
      62             : #endif
      63          23 : int cstat_get_client_names(struct cstat **clist, const char *clientconfdir)
      64             : {
      65          23 :         int i=0;
      66          23 :         int n=0;
      67          23 :         int ret=-1;
      68             :         struct cstat *c;
      69             :         struct cstat *cnew;
      70          23 :         struct dirent **dir=NULL;
      71             : 
      72          23 :         if(entries_in_directory_no_sort(clientconfdir, &dir, &n, 1 /*atime*/))
      73             :         {
      74             :                 logp("scandir failed for %s in %s: %s\n",
      75           1 :                         clientconfdir, __func__, strerror(errno));
      76           1 :                 goto end;
      77             :         }
      78          72 :         for(i=0; i<n; i++)
      79             :         {
      80             :                 // looks_like...() also avoids '.' and '..'.
      81          72 :                 if(looks_like_tmp_or_hidden_file(dir[i]->d_name))
      82             :                         continue;
      83         167 :                 for(c=*clist; c; c=c->next)
      84             :                 {
      85         114 :                         if(!c->name) continue;
      86         114 :                         if(!strcmp(dir[i]->d_name, c->name))
      87             :                                 break;
      88             :                 }
      89          70 :                 if(c) continue;
      90             : 
      91             :                 // We do not have this client yet. Add it.
      92          53 :                 if(!(cnew=cstat_alloc())
      93          53 :                   || cstat_init_with_cntr(cnew, dir[i]->d_name, clientconfdir))
      94             :                         goto end;
      95          53 :                 cstat_add_to_list(clist, cnew);
      96             :         }
      97             : 
      98             :         ret=0;
      99             : end:
     100          23 :         if(dir)
     101             :         {
     102          72 :                 for(i=0; i<n; i++) free_v((void **)&dir[i]);
     103          22 :                 free_v((void **)&dir);
     104             :         }
     105          23 :         return ret;
     106             : }
     107             : 
     108             : static void cstat_free_w(struct cstat **cstat)
     109             : {
     110           8 :         sdirs_free((struct sdirs **)&(*cstat)->sdirs);
     111           8 :         cstat_free(cstat);
     112             : }
     113             : 
     114             : #ifndef UTEST
     115             : static
     116             : #endif
     117           8 : void cstat_remove(struct cstat **clist, struct cstat **cstat)
     118             : {
     119             :         struct cstat *c;
     120           8 :         if(!cstat || !*cstat) return;
     121           8 :         if(*clist==*cstat)
     122             :         {
     123           4 :                 *clist=(*clist)->next;
     124           4 :                 if(*clist) (*clist)->prev=NULL;
     125             :                 cstat_free_w(cstat);
     126           4 :                 *cstat=*clist;
     127           4 :                 return;
     128             :         }
     129           7 :         for(c=*clist; c; c=c->next)
     130             :         {
     131           7 :                 if(c->next!=*cstat)
     132             :                         continue;
     133           4 :                 c->next=(*cstat)->next;
     134           4 :                 if(c->next)
     135           3 :                         c->next->prev=(*cstat)->prev;
     136             :                 cstat_free_w(cstat);
     137           4 :                 *cstat=*clist;
     138           4 :                 return;
     139             :         }
     140             : }
     141             : 
     142             : // Returns -1 on error, otherwise the number of clients that were reloaded.
     143             : #ifndef UTEST
     144             : static
     145             : #endif
     146          12 : int cstat_reload_from_client_confs(struct cstat **clist,
     147             :         struct conf **globalcs, struct conf **cconfs)
     148             : {
     149             :         struct cstat *c;
     150             :         struct stat statp;
     151             :         static time_t global_mtime=0;
     152          12 :         time_t global_mtime_new=0;
     153             :         const char *globalconffile;
     154          12 :         int reloaded=0;
     155             : 
     156          12 :         globalconffile=get_string(globalcs[OPT_CONFFILE]);
     157             : 
     158          12 :         if(stat(globalconffile, &statp)
     159          12 :           || !S_ISREG(statp.st_mode))
     160             :         {
     161             :                 logp("Could not stat main conf file %s: %s\n",
     162           1 :                         globalconffile, strerror(errno));
     163           1 :                 return -1;
     164             :         }
     165          11 :         global_mtime_new=statp.st_mtime;
     166             : 
     167             :         // FIX THIS: If '. included' conf files have changed, this code will
     168             :         // not detect them. I guess that conf.c should make a list of them.
     169             :         while(1)
     170             :         {
     171          41 :                 for(c=*clist; c; c=c->next)
     172             :                 {
     173             :                         // Look at the client conf files to see if they have
     174             :                         // changed, and reload bits and pieces if they have.
     175             : 
     176          31 :                         if(!c->conffile) continue;
     177          62 :                         if(stat(c->conffile, &statp)
     178          31 :                           || !S_ISREG(statp.st_mode))
     179             :                         {
     180           3 :                                 cstat_remove(clist, &c);
     181           3 :                                 break; // Go to the beginning of the list.
     182             :                         }
     183          28 :                         if(statp.st_mtime==c->conf_mtime
     184          12 :                           && global_mtime_new==global_mtime)
     185             :                         {
     186             :                                 // The conf files have not changed - no need to
     187             :                                 // do anything.
     188             :                                 continue;
     189             :                         }
     190          16 :                         c->conf_mtime=statp.st_mtime;
     191             : 
     192          16 :                         confs_free_content(cconfs);
     193          16 :                         if(set_string(cconfs[OPT_CNAME], c->name))
     194             :                                 return -1;
     195          16 :                         if(conf_load_clientconfdir(globalcs, cconfs))
     196             :                         {
     197             :                                 // If the file has junk in it, we will keep
     198             :                                 // trying to reload it after removal.
     199             :                                 // So, just deny permission to view it.
     200           1 :                                 c->permitted=0;
     201           1 :                                 continue;
     202             :                         }
     203             : 
     204          15 :                         if(set_cstat_from_conf(c, globalcs, cconfs))
     205             :                                 return -1;
     206          15 :                         reloaded++;
     207             :                 }
     208             :                 // Only stop if the end of the list was not reached.
     209          13 :                 if(!c) break;
     210             :         }
     211          11 :         if(global_mtime!=global_mtime_new)
     212           3 :                 global_mtime=global_mtime_new;
     213          11 :         return reloaded;
     214             : }
     215             : 
     216          16 : void cstat_set_run_status(struct cstat *cstat)
     217             : {
     218             :         struct stat statp;
     219          16 :         struct sdirs *sdirs=(struct sdirs *)cstat->sdirs;
     220          16 :         if(!cstat->permitted) return;
     221             : 
     222          28 :         if(lstat(sdirs->lock->path, &statp))
     223             :         {
     224          16 :                 if(lstat(sdirs->working, &statp))
     225           6 :                         cstat->run_status=RUN_STATUS_IDLE;
     226             :                 else
     227           2 :                         cstat->run_status=RUN_STATUS_CLIENT_CRASHED;
     228             :         }
     229             :         else
     230             :         {
     231           6 :                 if(!lock_test(sdirs->lock->path)) // Could have got lock.
     232           4 :                         cstat->run_status=RUN_STATUS_SERVER_CRASHED;
     233             :                 else
     234           2 :                         cstat->run_status=RUN_STATUS_RUNNING;
     235             :         }
     236             : 
     237             :         return;
     238             : }
     239             : 
     240             : // Return -1 on error, or the number of reloaded clients.
     241             : #ifndef UTEST
     242             : static
     243             : #endif
     244          10 : int reload_from_clientdir(struct cstat **clist)
     245             : {
     246          10 :         int reloaded=0;
     247             :         struct cstat *c;
     248          33 :         for(c=*clist; c; c=c->next)
     249             :         {
     250          23 :                 time_t ltime=0;
     251             :                 struct stat statp;
     252             :                 struct stat lstatp;
     253             :                 struct sdirs *sdirs;
     254             : 
     255          42 :                 if(!c->permitted) continue;
     256             : 
     257           8 :                 sdirs=(struct sdirs *)c->sdirs;
     258           8 :                 if(!sdirs || !sdirs->client) continue;
     259          16 :                 if(stat(sdirs->client, &statp))
     260             :                 {
     261             :                         // No clientdir.
     262           2 :                         if(!c->run_status)
     263           2 :                                 cstat_set_run_status(c);
     264             :                         continue;
     265             :                 }
     266          12 :                 if(!lstat(sdirs->lock->path, &lstatp))
     267           2 :                         ltime=lstatp.st_mtime;
     268           6 :                 if(statp.st_mtime==c->clientdir_mtime
     269           4 :                   && ltime==c->lockfile_mtime
     270           2 :                   && c->run_status!=RUN_STATUS_SERVER_CRASHED)
     271             :                   //&& !c->cntr)
     272             :                 {
     273             :                         // clientdir has not changed - no need to do anything.
     274             :                         continue;
     275             :                 }
     276           4 :                 c->clientdir_mtime=statp.st_mtime;
     277           4 :                 c->lockfile_mtime=ltime;
     278           4 :                 cstat_set_run_status(c);
     279             : 
     280           4 :                 bu_list_free(&c->bu);
     281             : // FIX THIS: should probably not load everything each time.
     282             : //              if(bu_get_current(sdirs, &c->bu))
     283             : //                      goto error;
     284           4 :                 if(bu_get_list_with_working(sdirs, &c->bu, c))
     285             :                         goto error;
     286           4 :                 reloaded++;
     287             :         }
     288             :         return reloaded;
     289             : error:
     290           0 :         return -1;
     291             : }
     292             : 
     293           2 : int cstat_load_data_from_disk(struct cstat **clist, struct conf **globalcs,
     294             :         struct conf **cconfs)
     295             : {
     296           2 :         if(!globalcs) return -1;
     297             :         return cstat_get_client_names(clist,
     298           2 :                 get_string(globalcs[OPT_CLIENTCONFDIR]))
     299           2 :           || cstat_reload_from_client_confs(clist, globalcs, cconfs)<0
     300           4 :           || reload_from_clientdir(clist)<0;
     301             : }
     302             : 
     303          26 : int cstat_set_backup_list(struct cstat *cstat)
     304             : {
     305          26 :         struct bu *bu=NULL;
     306             : 
     307             :         // Free any previous list.
     308          26 :         bu_list_free(&cstat->bu);
     309             : 
     310          26 :         if(!cstat->permitted) return 0;
     311             : 
     312          24 :         if(bu_get_list_with_working((struct sdirs *)cstat->sdirs, &bu, cstat))
     313             :         {
     314             :                 //logp("error when looking up current backups\n");
     315             :                 return 0;
     316             :         }
     317             : 
     318             :         // Find the end of the list just loaded, so we can traverse
     319             :         // it backwards later.
     320          50 :         while(bu && bu->next) bu=bu->next;
     321             : 
     322          23 :         cstat->bu=bu;
     323          23 :         return 0;
     324             : }

Generated by: LCOV version 1.10