~ubuntu-branches/ubuntu/precise/burp/precise

« back to all changes in this revision

Viewing changes to src/autoupgrade_client.c

  • Committer: Package Import Robot
  • Author(s): Bastiaan Franciscus van den Dikkenberg
  • Date: 2012-02-10 17:14:13 UTC
  • Revision ID: package-import@ubuntu.com-20120210171413-4du4hx4p49brijh4
Tags: upstream-1.3.0
ImportĀ upstreamĀ versionĀ 1.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "burp.h"
 
2
#include "prog.h"
 
3
#include "counter.h"
 
4
#include "msg.h"
 
5
#include "handy.h"
 
6
#include "cmd.h"
 
7
#include "asyncio.h"
 
8
#include "berrno.h"
 
9
#include "client_vss.h"
 
10
#include "autoupgrade_client.h"
 
11
 
 
12
static int receive_file(const char *autoupgrade_dir, const char *file, struct cntr *p1cntr)
 
13
{
 
14
        int ret=0;
 
15
#ifdef HAVE_WIN32
 
16
        BFILE bfd;
 
17
#else
 
18
        FILE *fp=NULL;
 
19
#endif
 
20
        char *incoming=NULL;
 
21
        unsigned long long rcvdbytes=0;
 
22
        unsigned long long sentbytes=0;
 
23
 
 
24
        if(!(incoming=prepend_s(autoupgrade_dir, file, strlen(file))))
 
25
        {
 
26
                ret=-1;
 
27
                goto end;
 
28
        }
 
29
#ifdef HAVE_WIN32
 
30
        binit(&bfd);
 
31
        bfd.use_backup_api=0;
 
32
        //set_win32_backup(&bfd);
 
33
        if(bopen(&bfd, incoming,
 
34
                O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
 
35
                S_IRUSR | S_IWUSR)<0)
 
36
        {
 
37
                berrno be;
 
38
                logp("Could not open for writing %s: %s\n",
 
39
                        incoming, be.bstrerror(errno));
 
40
                ret=-1;
 
41
                goto end;
 
42
        }
 
43
#else
 
44
        if(!(fp=open_file(incoming, "wb")))
 
45
        {
 
46
                ret=-1;
 
47
                goto end;
 
48
        }
 
49
#endif
 
50
 
 
51
#ifdef HAVE_WIN32
 
52
        ret=transfer_gzfile_in(incoming, &bfd, NULL, &rcvdbytes, &sentbytes,
 
53
                NULL, 0, p1cntr, NULL);
 
54
        bclose(&bfd);
 
55
#else
 
56
        ret=transfer_gzfile_in(incoming, NULL, fp, &rcvdbytes, &sentbytes,
 
57
                NULL, 0, p1cntr, NULL);
 
58
        close_fp(&fp);
 
59
#endif
 
60
end:
 
61
        if(!ret) logp("received: %s\n", incoming);
 
62
        if(incoming) free(incoming);
 
63
        return ret;
 
64
}
 
65
 
 
66
int autoupgrade_client(struct config *conf, struct cntr *p1cntr)
 
67
{
 
68
        int ret=-1;
 
69
        char *cp=NULL;
 
70
        char *copy=NULL;
 
71
        char *buf=NULL;
 
72
        size_t len=0;
 
73
        char cmd='\0';
 
74
        char *script_path=NULL;
 
75
        char script_name[32]="";
 
76
        char package_name[32]="";
 
77
        char write_str[256]="";
 
78
 
 
79
        if(!conf->autoupgrade_dir)
 
80
        {
 
81
                logp("autoupgrade_dir not set!\n");
 
82
                goto end;
 
83
        }
 
84
        if(!conf->autoupgrade_os)
 
85
        {
 
86
                logp("autoupgrade_os not set!\n");
 
87
                goto end;
 
88
        }
 
89
        if(!(copy=strdup(conf->autoupgrade_dir)))
 
90
        {
 
91
                logp("out of memory\n");
 
92
                goto end;
 
93
        }
 
94
        // strip trailing slash
 
95
        if(copy[strlen(copy)-1]=='/') copy[strlen(copy)-1]='\0';
 
96
        if((cp=strchr(copy, '/'))) *cp='\0';
 
97
        if(mkpath(&(conf->autoupgrade_dir), copy))
 
98
                goto end;
 
99
 
 
100
        // Let the server know we are ready.
 
101
        snprintf(write_str, sizeof(write_str),
 
102
                "autoupgrade:%s", conf->autoupgrade_os);
 
103
        if(async_write_str(CMD_GEN, write_str))
 
104
                goto end;
 
105
 
 
106
        if(async_read(&cmd, &buf, &len))
 
107
                goto end;
 
108
 
 
109
        if(cmd==CMD_GEN)
 
110
        {
 
111
                if(!strcmp(buf, "do not autoupgrade"))
 
112
                {
 
113
                        ret=0;
 
114
                        goto end;
 
115
                }
 
116
                else if(strcmp(buf, "autoupgrade ok"))
 
117
                {
 
118
                        logp("unexpected response to autoupgrade from server: %s\n", buf);
 
119
                        goto end;
 
120
                }
 
121
        }
 
122
        else
 
123
        {
 
124
                logp("unexpected response to autoupgrade from server: %c:%s\n", cmd, buf);
 
125
                goto end;
 
126
        }
 
127
 
 
128
#ifdef HAVE_WIN32
 
129
        win32_enable_backup_privileges(1 /* ignore_errors */);
 
130
        snprintf(script_name, sizeof(script_name), "script.bat");
 
131
        snprintf(package_name, sizeof(package_name), "package.exe");
 
132
#else
 
133
        snprintf(script_name, sizeof(script_name), "script");
 
134
        snprintf(package_name, sizeof(package_name), "package");
 
135
#endif
 
136
 
 
137
        if(receive_file(conf->autoupgrade_dir, script_name, p1cntr))
 
138
        {
 
139
                logp("Problem receiving %s/%s\n",
 
140
                        conf->autoupgrade_dir, script_name);
 
141
                goto end;
 
142
        }
 
143
        if(receive_file(conf->autoupgrade_dir, package_name, p1cntr))
 
144
        {
 
145
                logp("Problem receiving %s/%s\n",
 
146
                        conf->autoupgrade_dir, package_name);
 
147
                goto end;
 
148
        }
 
149
 
 
150
        if(!(script_path=prepend_s(conf->autoupgrade_dir,
 
151
                script_name, strlen(script_name)))) goto end;
 
152
 
 
153
        chmod(script_path, 0755);
 
154
 
 
155
        /* Run the script here. */
 
156
        ret=run_script(script_path,
 
157
                NULL, 0, NULL, NULL, NULL, NULL, NULL, p1cntr,
 
158
                0 /* do not wait */);
 
159
        /* To get round Windows problems to do with installing over files
 
160
           that the current process is running from, I am forking the child,
 
161
           then immediately exiting the parent process. */
 
162
 
 
163
        printf("\n");
 
164
        logp("The server tried to upgrade your client.\n");
 
165
        logp("You will need to try your command again.\n");
 
166
        async_free();
 
167
 
 
168
        exit(0);
 
169
end:
 
170
        if(copy) free(copy);
 
171
        if(buf) free(buf);
 
172
        if(script_path) free(script_path);
 
173
        return ret;
 
174
}