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