Line data Source code
1 : #include "../burp.h"
2 : #include "../action.h"
3 : #include "../alloc.h"
4 : #include "../asfd.h"
5 : #include "../async.h"
6 : #include "../conf.h"
7 : #include "../cmd.h"
8 : #include "../cntr.h"
9 : #include "../fsops.h"
10 : #include "../handy.h"
11 : #include "../iobuf.h"
12 : #include "../log.h"
13 : #include "../run_script.h"
14 : #include "auth.h"
15 : #include "backup_phase1.h"
16 : #include "backup_phase3.h"
17 : #include "compress.h"
18 : #include "delete.h"
19 : #include "protocol1/backup_phase2.h"
20 : #include "protocol1/backup_phase4.h"
21 : #include "protocol2/backup_phase2.h"
22 : #include "protocol2/backup_phase4.h"
23 : #include "protocol2/champ_chooser/champ_client.h"
24 :
25 0 : static int open_log(struct asfd *asfd,
26 : struct sdirs *sdirs, struct conf **cconfs)
27 : {
28 0 : int ret=-1;
29 0 : char *logpath=NULL;
30 0 : const char *peer_version=get_string(cconfs[OPT_PEER_VERSION]);
31 :
32 0 : if(!(logpath=prepend_s(sdirs->rworking, "log"))) goto end;
33 0 : if(log_fzp_set(logpath, cconfs))
34 : {
35 0 : logp("could not open log file: %s\n", logpath);
36 0 : goto end;
37 : }
38 :
39 0 : logp("Client version: %s\n", peer_version?:"");
40 0 : logp("Protocol: %d\n", (int)get_protocol(cconfs));
41 0 : if(get_int(cconfs[OPT_CLIENT_IS_WINDOWS]))
42 0 : logp("Client is Windows\n");
43 :
44 : // Make sure a warning appears in the backup log.
45 : // The client will already have been sent a message with logw.
46 : // This time, prevent it sending a logw to the client by specifying
47 : // NULL for cntr.
48 0 : if(get_int(cconfs[OPT_VERSION_WARN])) version_warn(asfd, NULL, cconfs);
49 :
50 0 : ret=0;
51 : end:
52 0 : free_w(&logpath);
53 0 : return ret;
54 : }
55 :
56 0 : static int write_incexc(const char *realworking, const char *incexc)
57 : {
58 0 : int ret=-1;
59 0 : struct fzp *fzp=NULL;
60 0 : char *path=NULL;
61 0 : if(!incexc || !*incexc) return 0;
62 0 : if(!(path=prepend_s(realworking, "incexc"))
63 0 : || !(fzp=fzp_open(path, "wb")))
64 0 : goto end;
65 0 : fzp_printf(fzp, "%s", incexc);
66 0 : ret=0;
67 : end:
68 0 : if(fzp_close(&fzp))
69 : {
70 0 : logp("error writing to %s in write_incexc\n", path);
71 0 : ret=-1;
72 : }
73 0 : free_w(&path);
74 0 : return ret;
75 : }
76 :
77 0 : static int backup_phase1_server(struct async *as,
78 : struct sdirs *sdirs, struct conf **cconfs)
79 : {
80 0 : int breaking=get_int(cconfs[OPT_BREAKPOINT]);
81 0 : if(breaking==1)
82 0 : return breakpoint(breaking, __func__);
83 0 : return backup_phase1_server_all(as, sdirs, cconfs);
84 : }
85 :
86 0 : static int backup_phase2_server(struct async *as, struct sdirs *sdirs,
87 : const char *incexc, int resume, struct conf **cconfs)
88 : {
89 0 : int breaking=get_int(cconfs[OPT_BREAKPOINT]);
90 0 : if(breaking==2)
91 0 : return breakpoint(breaking, __func__);
92 :
93 0 : switch(get_protocol(cconfs))
94 : {
95 : case PROTO_1:
96 : return backup_phase2_server_protocol1(as, sdirs,
97 0 : incexc, resume, cconfs);
98 : default:
99 : return backup_phase2_server_protocol2(as, sdirs,
100 0 : resume, cconfs);
101 : }
102 : }
103 :
104 0 : static int backup_phase3_server(struct sdirs *sdirs, struct conf **cconfs)
105 : {
106 0 : int breaking=get_int(cconfs[OPT_BREAKPOINT]);
107 0 : if(breaking==3)
108 0 : return breakpoint(breaking, __func__);
109 :
110 0 : return backup_phase3_server_all(sdirs, cconfs);
111 : }
112 :
113 0 : static int backup_phase4_server(struct sdirs *sdirs, struct conf **cconfs)
114 : {
115 0 : int breaking=get_int(cconfs[OPT_BREAKPOINT]);
116 0 : if(breaking==4)
117 0 : return breakpoint(breaking, __func__);
118 :
119 0 : log_fzp_set(NULL, cconfs);
120 : // Phase4 will open logfp again (in case it is resuming).
121 0 : switch(get_protocol(cconfs))
122 : {
123 : case PROTO_1:
124 0 : return backup_phase4_server_protocol1(sdirs, cconfs);
125 : default:
126 0 : return backup_phase4_server_protocol2(sdirs, cconfs);
127 : }
128 : }
129 :
130 0 : static void log_rshash(struct conf **confs)
131 : {
132 0 : if(get_protocol(confs)!=PROTO_1) return;
133 : logp("Using librsync hash %s\n",
134 0 : rshash_to_str(get_e_rshash(confs[OPT_RSHASH])));
135 : }
136 :
137 0 : static int do_backup_server(struct async *as, struct sdirs *sdirs,
138 : struct conf **cconfs, const char *incexc, int resume)
139 : {
140 0 : int ret=0;
141 0 : int do_phase2=1;
142 0 : struct asfd *chfd=NULL;
143 0 : struct asfd *asfd=as->asfd;
144 0 : enum protocol protocol=get_protocol(cconfs);
145 :
146 0 : logp("in do_backup_server\n");
147 :
148 0 : log_rshash(cconfs);
149 :
150 0 : if(resume)
151 : {
152 0 : if(sdirs_get_real_working_from_symlink(sdirs)
153 0 : || sdirs_get_real_manifest(sdirs, protocol)
154 0 : || open_log(asfd, sdirs, cconfs))
155 0 : goto error;
156 : }
157 : else
158 : {
159 : // Not resuming - need to set everything up fresh.
160 0 : if(sdirs_create_real_working(sdirs,
161 0 : get_string(cconfs[OPT_TIMESTAMP_FORMAT]))
162 0 : || sdirs_get_real_manifest(sdirs, protocol)
163 0 : || open_log(asfd, sdirs, cconfs))
164 0 : goto error;
165 :
166 0 : if(write_incexc(sdirs->rworking, incexc))
167 : {
168 0 : logp("unable to write incexc\n");
169 0 : goto error;
170 : }
171 :
172 0 : if(backup_phase1_server(as, sdirs, cconfs))
173 : {
174 0 : logp("error in phase 1\n");
175 0 : goto error;
176 : }
177 : }
178 :
179 0 : if(protocol==PROTO_2
180 0 : && !(chfd=champ_chooser_connect(as, sdirs, cconfs)))
181 : {
182 0 : logp("problem connecting to champ chooser\n");
183 0 : goto error;
184 : }
185 :
186 0 : if(resume)
187 : {
188 : struct stat statp;
189 0 : if(lstat(sdirs->phase1data, &statp)
190 0 : && !lstat(sdirs->changed, &statp)
191 0 : && !lstat(sdirs->unchanged, &statp))
192 : {
193 : // In this condition, it looks like there was an
194 : // interruption during phase3. Skip phase2.
195 0 : do_phase2=0;
196 : }
197 : }
198 :
199 0 : if(do_phase2)
200 : {
201 0 : if(backup_phase2_server(as, sdirs, incexc, resume, cconfs))
202 : {
203 0 : logp("error in backup phase 2\n");
204 0 : goto error;
205 : }
206 :
207 0 : asfd->write_str(asfd, CMD_GEN, "okbackupend");
208 : }
209 :
210 : // Close the connection with the client, the rest of the job we can do
211 : // by ourselves.
212 0 : logp("Backup ending - disconnect from client.\n");
213 0 : as->asfd_remove(as, asfd);
214 0 : asfd_close(asfd);
215 :
216 0 : if(backup_phase3_server(sdirs, cconfs))
217 : {
218 0 : logp("error in backup phase 3\n");
219 0 : goto error;
220 : }
221 :
222 0 : if(do_rename(sdirs->working, sdirs->finishing))
223 0 : goto error;
224 :
225 0 : if(backup_phase4_server(sdirs, cconfs))
226 : {
227 0 : logp("error in backup phase 4\n");
228 0 : goto error;
229 : }
230 :
231 0 : cntr_print(get_cntr(cconfs), ACTION_BACKUP);
232 : cntr_stats_to_file(get_cntr(cconfs),
233 0 : sdirs->rworking, ACTION_BACKUP, cconfs);
234 :
235 : // Move the symlink to indicate that we are now in the end phase. The
236 : // rename() race condition is automatically recoverable here.
237 0 : if(do_rename(sdirs->finishing, sdirs->current)) goto error;
238 :
239 0 : logp("Backup completed.\n");
240 0 : log_fzp_set(NULL, cconfs);
241 : compress_filename(sdirs->rworking,
242 0 : "log", "log.gz", get_int(cconfs[OPT_COMPRESSION]));
243 :
244 0 : goto end;
245 : error:
246 0 : ret=-1;
247 : end:
248 :
249 0 : log_fzp_set(NULL, cconfs);
250 0 : if(chfd) as->asfd_remove(as, chfd);
251 0 : asfd_free(&chfd);
252 0 : return ret;
253 : }
254 :
255 0 : int run_backup(struct async *as, struct sdirs *sdirs, struct conf **cconfs,
256 : const char *incexc, int *timer_ret, int resume)
257 : {
258 : int ret;
259 0 : char okstr[32]="";
260 0 : struct asfd *asfd=as->asfd;
261 0 : struct iobuf *rbuf=asfd->rbuf;
262 0 : const char *cname=get_string(cconfs[OPT_CNAME]);
263 :
264 0 : if(get_string(cconfs[OPT_RESTORE_CLIENT]))
265 : {
266 : // This client is not the original client, so a backup might
267 : // cause all sorts of trouble.
268 0 : logp("Not allowing backup of %s\n", cname);
269 0 : return asfd->write_str(asfd, CMD_GEN, "Backup is not allowed");
270 : }
271 :
272 : // Set quality of service bits on backups.
273 0 : asfd->set_bulk_packets(asfd);
274 :
275 0 : if(!strncmp_w(rbuf->buf, "backupphase1timed"))
276 : {
277 0 : int a=0;
278 : const char *args[12];
279 0 : int checkonly=!strncmp_w(rbuf->buf, "backupphase1timedcheck");
280 0 : if(checkonly) logp("Client asked for a timer check only.\n");
281 :
282 0 : args[a++]=get_string(cconfs[OPT_TIMER_SCRIPT]);
283 0 : args[a++]=cname;
284 0 : args[a++]=sdirs->current;
285 0 : args[a++]=sdirs->clients;
286 0 : args[a++]="reserved1";
287 0 : args[a++]="reserved2";
288 0 : args[a++]=NULL;
289 0 : if((*timer_ret=run_script(asfd, args,
290 0 : get_strlist(cconfs[OPT_TIMER_ARG]),
291 : cconfs,
292 : 1 /* wait */,
293 : 1 /* use logp */,
294 : 0 /* no log_remote */
295 0 : ))<0)
296 : {
297 : logp("Error running timer script for %s\n",
298 0 : cname);
299 0 : return *timer_ret;
300 : }
301 0 : if(*timer_ret)
302 : {
303 0 : if(!checkonly)
304 : logp("Not running backup of %s\n",
305 0 : cname);
306 : return asfd->write_str(asfd,
307 0 : CMD_GEN, "timer conditions not met");
308 : }
309 0 : if(checkonly)
310 : {
311 0 : logp("Client asked for a timer check only,\n");
312 0 : logp("so a backup is not happening right now.\n");
313 : return asfd->write_str(asfd,
314 0 : CMD_GEN, "timer conditions met");
315 : }
316 0 : logp("Running backup of %s\n", cname);
317 : }
318 0 : else if(!get_int(cconfs[OPT_CLIENT_CAN_FORCE_BACKUP]))
319 : {
320 0 : logp("Not allowing forced backup of %s\n", cname);
321 : return asfd->write_str(asfd,
322 0 : CMD_GEN, "Forced backup is not allowed");
323 : }
324 :
325 : snprintf(okstr, sizeof(okstr), "%s:%d",
326 0 : resume?"resume":"ok", get_int(cconfs[OPT_COMPRESSION]));
327 0 : if(asfd->write_str(asfd, CMD_GEN, okstr)) return -1;
328 :
329 0 : if((ret=do_backup_server(as, sdirs, cconfs, incexc, resume)))
330 0 : goto end;
331 0 : if((ret=delete_backups(sdirs, cname,
332 0 : get_strlist(cconfs[OPT_KEEP]),
333 0 : get_string(cconfs[OPT_MANUAL_DELETE]))))
334 0 : goto end;
335 0 : if(get_protocol(cconfs)==PROTO_2)
336 0 : ret=regenerate_client_dindex(sdirs);
337 : end:
338 0 : return ret;
339 : }
|