LCOV - code coverage report
Current view: top level - src/server - autoupgrade.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 19 66 28.8 %
Date: 2017-01-01 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include "../burp.h"
       2             : #include "../asfd.h"
       3             : #include "../async.h"
       4             : #include "../cmd.h"
       5             : #include "../handy.h"
       6             : #include "../log.h"
       7             : #include "../prepend.h"
       8             : #include "autoupgrade.h"
       9             : 
      10             : // Return -1 on error or success, 0 to continue normally.
      11           1 : int autoupgrade_server(struct async *as,
      12             :         long ser_ver, long cli_ver, const char *os, struct cntr *cntr,
      13             :         const char *autoupgrade_dir)
      14             : {
      15           1 :         int ret=-1;
      16           1 :         char *path=NULL;
      17           1 :         char *base_path=NULL;
      18           1 :         char *script_path=NULL;
      19           1 :         char *package_path=NULL;
      20           1 :         char *script_path_top=NULL;
      21           1 :         char *script_path_specific=NULL;
      22             :         struct stat stats;
      23             :         struct stat statp;
      24             :         struct asfd *asfd;
      25           1 :         asfd=as->asfd;
      26             : 
      27           1 :         if(!autoupgrade_dir)
      28             :         {
      29             :                 // Autoupgrades not turned on on the server.
      30           1 :                 ret=0;
      31           1 :                 asfd->write_str(asfd, CMD_GEN, "do not autoupgrade");
      32           1 :                 goto end;
      33             :         }
      34             : 
      35           0 :         if(cli_ver>=ser_ver)
      36             :         {
      37             :                 // No need to upgrade - client is same version as server,
      38             :                 // or newer.
      39           0 :                 ret=0;
      40           0 :                 asfd->write_str(asfd, CMD_GEN, "do not autoupgrade");
      41           0 :                 goto end;
      42             :         }
      43             : 
      44           0 :         if(!(base_path=prepend_s(autoupgrade_dir, os))
      45           0 :           || !(path=prepend_s(base_path, VERSION))
      46           0 :           || !(script_path_top=prepend_s(base_path, "script"))
      47           0 :           || !(script_path_specific=prepend_s(path, "script"))
      48           0 :           || !(package_path=prepend_s(path, "package")))
      49             :         {
      50           0 :                 asfd->write_str(asfd, CMD_GEN, "do not autoupgrade");
      51           0 :                 goto end;
      52             :         }
      53             : 
      54           0 :         if(!stat(script_path_specific, &stats))
      55           0 :                 script_path=script_path_specific;
      56           0 :         else if(!stat(script_path_top, &stats))
      57           0 :                 script_path=script_path_top;
      58             :         else
      59             :         {
      60           0 :                 logp("Want to autoupgrade client, but no file at:\n");
      61           0 :                 logp("%s\n", script_path_top);
      62           0 :                 logp("or:\n");
      63           0 :                 logp("%s\n", script_path_specific);
      64           0 :                 ret=0; // this is probably OK
      65           0 :                 asfd->write_str(asfd, CMD_GEN, "do not autoupgrade");
      66           0 :                 goto end;
      67             :         }
      68           0 :         if(stat(package_path, &statp))
      69             :         {
      70           0 :                 logp("Want to autoupgrade client, but no file available at:\n");
      71           0 :                 logp("%s\n", package_path);
      72           0 :                 ret=0; // this is probably OK
      73           0 :                 asfd->write_str(asfd, CMD_GEN, "do not autoupgrade");
      74           0 :                 goto end;
      75             :         }
      76             : 
      77           0 :         if(!S_ISREG(stats.st_mode))
      78             :         {
      79           0 :                 logp("%s is not a regular file\n", script_path);
      80           0 :                 asfd->write_str(asfd, CMD_GEN, "do not autoupgrade");
      81           0 :                 goto end;
      82             :         }
      83           0 :         if(!S_ISREG(statp.st_mode))
      84             :         {
      85           0 :                 logp("%s is not a regular file\n", package_path);
      86           0 :                 asfd->write_str(asfd, CMD_GEN, "do not autoupgrade");
      87           0 :                 goto end;
      88             :         }
      89             : 
      90           0 :         if(asfd->write_str(asfd, CMD_GEN, "autoupgrade ok"))
      91             :                 goto end;
      92             : 
      93           0 :         if(send_a_file(asfd, script_path, cntr))
      94             :         {
      95           0 :                 logp("Problem sending %s\n", script_path);
      96           0 :                 goto end;
      97             :         }
      98           0 :         if(send_a_file(asfd, package_path, cntr))
      99             :         {
     100           0 :                 logp("Problem sending %s\n", package_path);
     101           0 :                 goto end;
     102             :         }
     103           0 :         if(asfd_flush_asio(asfd))
     104             :                 goto end;
     105             :         /* Clients currently exit after forking, so exit ourselves. */
     106           0 :         logp("Expecting client to upgrade - now exiting\n");
     107           0 :         asfd_free(&as->asfd);
     108           0 :         exit(0);
     109             : end:
     110           1 :         free_w(&path);
     111           1 :         free_w(&base_path);
     112           1 :         free_w(&script_path_specific);
     113           1 :         free_w(&script_path_top);
     114           1 :         free_w(&package_path);
     115           1 :         return ret;
     116             : }

Generated by: LCOV version 1.10