LCOV - code coverage report
Current view: top level - src/client - autoupgrade.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 0 61 0.0 %
Date: 2016-05-30 Functions: 0 3 0.0 %

          Line data    Source code
       1             : #include "../burp.h"
       2             : #include "../asfd.h"
       3             : #include "../async.h"
       4             : #include "../cmd.h"
       5             : #include "../fsops.h"
       6             : #include "../iobuf.h"
       7             : #include "../handy.h"
       8             : #include "../log.h"
       9             : #include "../prepend.h"
      10             : #include "../run_script.h"
      11             : #include "cvss.h"
      12             : 
      13           0 : static int receive_file(struct asfd *asfd, const char *autoupgrade_dir,
      14             :         const char *file, struct cntr *cntr)
      15             : {
      16           0 :         int ret=0;
      17           0 :         char *incoming=NULL;
      18           0 :         if(!(incoming=prepend_s(autoupgrade_dir, file))) return -1;
      19           0 :         ret=receive_a_file(asfd, incoming, cntr);
      20           0 :         free_w(&incoming);
      21           0 :         return ret;
      22             : }
      23             : 
      24           0 : static enum asl_ret autoupgrade_func(struct asfd *asfd,
      25             :         struct conf **confs, void *param)
      26             : {
      27           0 :         if(!strcmp(asfd->rbuf->buf, "do not autoupgrade"))
      28             :                 return ASL_END_OK;
      29           0 :         if(strcmp(asfd->rbuf->buf, "autoupgrade ok"))
      30             :         {
      31           0 :                 iobuf_log_unexpected(asfd->rbuf, __func__);
      32           0 :                 return ASL_END_ERROR;
      33             :         }
      34             :         return ASL_END_OK_RETURN_1;
      35             : }
      36             : 
      37           0 : int autoupgrade_client(struct async *as, struct conf **confs)
      38             : {
      39           0 :         int a=0;
      40           0 :         int ret=-1;
      41           0 :         char *cp=NULL;
      42           0 :         char *copy=NULL;
      43           0 :         char *script_path=NULL;
      44           0 :         char script_name[32]="";
      45           0 :         char package_name[32]="";
      46           0 :         char write_str[256]="";
      47             :         const char *args[2];
      48           0 :         struct iobuf *rbuf=NULL;
      49             :         struct asfd *asfd;
      50           0 :         char *autoupgrade_dir=get_string(confs[OPT_AUTOUPGRADE_DIR]);
      51           0 :         const char *autoupgrade_os=get_string(confs[OPT_AUTOUPGRADE_OS]);
      52           0 :         struct cntr *cntr=get_cntr(confs);
      53           0 :         asfd=as->asfd;
      54             : 
      55           0 :         if(!autoupgrade_dir)
      56             :         {
      57           0 :                 logp("autoupgrade_dir not set!\n");
      58           0 :                 goto end;
      59             :         }
      60           0 :         if(!autoupgrade_os)
      61             :         {
      62           0 :                 logp("autoupgrade_os not set!\n");
      63           0 :                 goto end;
      64             :         }
      65           0 :         if(!(copy=strdup_w(autoupgrade_dir, __func__)))
      66             :                 goto end;
      67             : 
      68           0 :         strip_trailing_slashes(&copy);
      69           0 :         if((cp=strchr(copy, '/'))) *cp='\0';
      70           0 :         if(mkpath(&autoupgrade_dir, copy))
      71             :                 goto end;
      72             : 
      73             :         // Let the server know we are ready.
      74             :         snprintf(write_str, sizeof(write_str),
      75             :                 "autoupgrade:%s", autoupgrade_os);
      76           0 :         if(asfd->write_str(asfd, CMD_GEN, write_str))
      77             :                 goto end;
      78             : 
      79           0 :         if(!(a=asfd->simple_loop(asfd,
      80           0 :                 confs, NULL, __func__, autoupgrade_func)))
      81             :         {
      82             :                 ret=0; // No autoupgrade.
      83             :                 goto end;
      84             :         }
      85           0 :         else if(a<0) // Error.
      86             : 
      87             : #ifdef HAVE_WIN32
      88             :         win32_enable_backup_privileges();
      89             :         snprintf(script_name, sizeof(script_name), "script.bat");
      90             :         snprintf(package_name, sizeof(package_name), "package.exe");
      91             : #else
      92             :         snprintf(script_name, sizeof(script_name), "script");
      93             :         snprintf(package_name, sizeof(package_name), "package");
      94             : #endif
      95             : 
      96           0 :         if(receive_file(asfd, autoupgrade_dir, script_name, cntr))
      97             :         {
      98             :                 logp("Problem receiving %s/%s\n",
      99           0 :                         autoupgrade_dir, script_name);
     100           0 :                 goto end;
     101             :         }
     102           0 :         if(receive_file(asfd, autoupgrade_dir, package_name, cntr))
     103             :         {
     104             :                 logp("Problem receiving %s/%s\n",
     105           0 :                         autoupgrade_dir, package_name);
     106           0 :                 goto end;
     107             :         }
     108             : 
     109           0 :         if(!(script_path=prepend_s(autoupgrade_dir, script_name)))
     110             :                 goto end;
     111             : 
     112           0 :         chmod(script_path, 0755);
     113             : 
     114             :         /* Run the script here. */
     115           0 :         a=0;
     116           0 :         args[a++]=script_path;
     117           0 :         args[a++]=NULL;
     118             :         run_script(asfd, args, NULL, confs,
     119           0 :                 0 /* do not wait */, 1 /* use logp */, 1 /* log_remote */);
     120             :         /* To get round Windows problems to do with installing over files
     121             :            that the current process is running from, I am forking the child,
     122             :            then immediately exiting the parent process. */
     123             : 
     124             :         printf("\n");
     125           0 :         logp("The server tried to upgrade your client.\n");
     126           0 :         logp("You will need to try your command again.\n");
     127           0 :         asfd_flush_asio(asfd);
     128           0 :         asfd_free(&as->asfd);
     129             : 
     130           0 :         exit(0);
     131             : end:
     132           0 :         free_w(&copy);
     133           0 :         free_w(&script_path);
     134           0 :         iobuf_free(&rbuf);
     135           0 :         return ret;
     136             : }

Generated by: LCOV version 1.10