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

Generated by: LCOV version 1.13