Line data Source code
1 : #include "../burp.h"
2 : #include "../action.h"
3 : #include "../asfd.h"
4 : #include "../async.h"
5 : #include "../cmd.h"
6 : #include "../cntr.h"
7 : #include "../handy.h"
8 : #include "../fsops.h"
9 : #include "../iobuf.h"
10 : #include "../lock.h"
11 : #include "../log.h"
12 : #include "../regexp.h"
13 : #include "../run_script.h"
14 : #include "backup.h"
15 : #include "delete.h"
16 : #include "diff.h"
17 : #include "list.h"
18 : #include "protocol2/restore.h"
19 : #include "restore.h"
20 : #include "rubble.h"
21 : #include "sdirs.h"
22 :
23 : // FIX THIS: Somewhat haphazard.
24 : /* Return 0 for everything OK. -1 for error, or 1 to mean that there was
25 : another process that has the lock. */
26 0 : static int get_lock_sdirs(struct asfd *asfd, struct sdirs *sdirs)
27 : {
28 : struct stat statp;
29 :
30 : // Make sure the lock directory exists.
31 0 : if(mkpath(&sdirs->lock->path, sdirs->lockdir))
32 : {
33 0 : asfd->write_str(asfd, CMD_ERROR, "problem with lock directory");
34 0 : goto error;
35 : }
36 :
37 0 : lock_get(sdirs->lock);
38 0 : switch(sdirs->lock->status)
39 : {
40 : case GET_LOCK_GOT: break;
41 : case GET_LOCK_NOT_GOT:
42 0 : if(!lstat(sdirs->finishing, &statp))
43 : {
44 0 : char msg[256]="";
45 0 : logp("finalising previous backup\n");
46 : snprintf(msg, sizeof(msg),
47 : "Finalising previous backup of client. "
48 : "Please try again later.");
49 0 : asfd->write_str(asfd, CMD_ERROR, msg);
50 : }
51 : else
52 : {
53 0 : logp("Another instance of client is already running.\n");
54 : asfd->write_str(asfd, CMD_ERROR,
55 0 : "another instance is already running");
56 : }
57 : goto lockedout;
58 : case GET_LOCK_ERROR:
59 : default:
60 : logp("Problem with lock file on server: %s\n",
61 0 : sdirs->lock->path);
62 : asfd->write_str(asfd, CMD_ERROR,
63 0 : "problem with lock file on server");
64 0 : goto error;
65 : }
66 :
67 : return 0;
68 : lockedout:
69 : return 1;
70 : error:
71 : return -1;
72 : }
73 :
74 0 : static int client_can_generic(struct conf **cconfs, enum conf_opt o)
75 : {
76 : // Always allow restore_clients, unless we are talking about forcing
77 : // a backup.
78 0 : if(get_string(cconfs[OPT_RESTORE_CLIENT])
79 0 : && o!=OPT_CLIENT_CAN_FORCE_BACKUP)
80 : return 1;
81 :
82 0 : return get_int(cconfs[o]);
83 : }
84 :
85 0 : static int client_can_restore(struct conf **cconfs)
86 : {
87 : struct stat statp;
88 0 : const char *restore_path=get_string(cconfs[OPT_RESTORE_PATH]);
89 :
90 : // If there is a restore file on the server, it is always OK.
91 0 : if(restore_path
92 0 : && !lstat(restore_path, &statp))
93 : {
94 : // Remove the file.
95 0 : unlink(restore_path);
96 0 : return 1;
97 : }
98 :
99 0 : return client_can_generic(cconfs, OPT_CLIENT_CAN_RESTORE);
100 : }
101 :
102 0 : static void maybe_do_notification(struct asfd *asfd,
103 : int status, const char *clientdir,
104 : const char *storagedir, const char *filename,
105 : const char *brv, struct conf **cconfs)
106 : {
107 0 : int a=0;
108 : const char *args[12];
109 0 : struct cntr *cntr=get_cntr(cconfs);
110 0 : args[a++]=NULL; // Fill in the script name later.
111 0 : args[a++]=get_string(cconfs[OPT_CNAME]);
112 0 : args[a++]=clientdir;
113 0 : args[a++]=storagedir;
114 0 : args[a++]=filename;
115 0 : args[a++]=brv;
116 0 : if(status)
117 : {
118 0 : args[0]=get_string(cconfs[OPT_N_FAILURE_SCRIPT]);
119 0 : args[a++]="0";
120 0 : args[a++]=NULL;
121 : run_script(asfd, args, get_strlist(cconfs[OPT_N_FAILURE_ARG]),
122 0 : cconfs, 1, 1, 1);
123 : }
124 0 : else if((get_int(cconfs[OPT_N_SUCCESS_WARNINGS_ONLY])
125 0 : && cntr->ent[CMD_WARNING]->count > 0)
126 0 : || (get_int(cconfs[OPT_N_SUCCESS_CHANGES_ONLY])
127 0 : && cntr->ent[CMD_TOTAL]->changed > 0)
128 0 : || (!get_int(cconfs[OPT_N_SUCCESS_WARNINGS_ONLY])
129 0 : && !get_int(cconfs[OPT_N_SUCCESS_CHANGES_ONLY])))
130 : {
131 0 : char warnings[32]="";
132 : snprintf(warnings, sizeof(warnings), "%"PRIu64,
133 0 : cntr->ent[CMD_WARNING]->count);
134 0 : args[0]=get_string(cconfs[OPT_N_SUCCESS_SCRIPT]);
135 0 : args[a++]=warnings;
136 0 : args[a++]=NULL;
137 : run_script(asfd, args, get_strlist(cconfs[OPT_N_SUCCESS_ARG]),
138 0 : cconfs, 1, 1, 1);
139 : }
140 0 : }
141 :
142 0 : static int run_restore(struct asfd *asfd,
143 : struct sdirs *sdirs, struct conf **cconfs, int srestore)
144 : {
145 0 : int ret=-1;
146 0 : char *cp=NULL;
147 0 : char *copy=NULL;
148 : enum action act;
149 0 : char *backupnostr=NULL;
150 0 : char *restoreregex=NULL;
151 0 : char *dir_for_notify=NULL;
152 0 : struct iobuf *rbuf=asfd->rbuf;
153 0 : const char *cname=get_string(cconfs[OPT_CNAME]);
154 :
155 0 : if(!(copy=strdup_w(rbuf->buf, __func__)))
156 : goto end;
157 :
158 0 : iobuf_free_content(rbuf);
159 :
160 0 : if(!strncmp_w(copy, "restore ")) act=ACTION_RESTORE;
161 0 : else act=ACTION_VERIFY;
162 :
163 0 : if(!(backupnostr=strchr(copy, ' ')))
164 : {
165 0 : logp("Could not parse %s in %s\n", copy, __func__);
166 0 : goto end;
167 : }
168 0 : backupnostr++;
169 0 : if((cp=strchr(backupnostr, ':'))) *cp='\0';
170 0 : if(set_string(cconfs[OPT_BACKUP], backupnostr))
171 : goto end;
172 :
173 0 : if(act==ACTION_RESTORE)
174 : {
175 : int r;
176 0 : if((r=client_can_restore(cconfs))<0)
177 : goto end;
178 0 : else if(!r)
179 : {
180 0 : logp("Not allowing restore of %s\n", cname);
181 0 : if(!asfd->write_str(asfd, CMD_GEN,
182 0 : "Client restore is not allowed")) ret=0;
183 : goto end;
184 : }
185 : }
186 0 : if(act==ACTION_VERIFY
187 0 : && !(client_can_generic(cconfs, OPT_CLIENT_CAN_VERIFY)))
188 : {
189 0 : logp("Not allowing verify of %s\n", cname);
190 0 : if(!asfd->write_str(asfd, CMD_GEN,
191 0 : "Client verify is not allowed")) ret=0;
192 : goto end;
193 : }
194 :
195 0 : if((restoreregex=strchr(copy, ':')))
196 : {
197 0 : *restoreregex='\0';
198 0 : restoreregex++;
199 : }
200 0 : if(restoreregex && *restoreregex
201 0 : && set_string(cconfs[OPT_REGEX], restoreregex))
202 : goto end;
203 0 : if(asfd->write_str(asfd, CMD_GEN, "ok"))
204 : goto end;
205 : ret=do_restore_server(asfd, sdirs, act,
206 0 : srestore, &dir_for_notify, cconfs);
207 0 : if(dir_for_notify)
208 : maybe_do_notification(asfd, ret,
209 : sdirs->client, dir_for_notify,
210 : act==ACTION_RESTORE?"restorelog":"verifylog",
211 : act==ACTION_RESTORE?"restore":"verify",
212 0 : cconfs);
213 : end:
214 0 : free_w(©);
215 0 : free_w(&dir_for_notify);
216 0 : return ret;
217 : }
218 :
219 0 : static int run_delete(struct asfd *asfd,
220 : struct sdirs *sdirs, struct conf **cconfs)
221 : {
222 0 : char *backupno=NULL;
223 0 : struct iobuf *rbuf=asfd->rbuf;
224 0 : const char *cname=get_string(cconfs[OPT_CNAME]);
225 0 : if(!client_can_generic(cconfs, OPT_CLIENT_CAN_DELETE))
226 : {
227 0 : logp("Not allowing delete of %s\n", cname);
228 0 : asfd->write_str(asfd, CMD_GEN, "Client delete is not allowed");
229 0 : return -1;
230 : }
231 0 : backupno=rbuf->buf+strlen("delete ");
232 : return do_delete_server(asfd, sdirs,
233 : get_cntr(cconfs), cname, backupno,
234 0 : get_string(cconfs[OPT_MANUAL_DELETE]));
235 : }
236 :
237 0 : static int run_list(struct asfd *asfd,
238 : struct sdirs *sdirs, struct conf **cconfs)
239 : {
240 0 : int ret=-1;
241 0 : char *cp=NULL;
242 0 : char *backupno=NULL;
243 0 : char *browsedir=NULL;
244 0 : char *listregex=NULL;
245 0 : struct iobuf *rbuf=asfd->rbuf;
246 :
247 0 : if(!client_can_generic(cconfs, OPT_CLIENT_CAN_LIST))
248 : {
249 : logp("Not allowing list of %s\n",
250 0 : get_string(cconfs[OPT_CNAME]));
251 0 : asfd->write_str(asfd, CMD_GEN, "Client list is not allowed");
252 0 : goto end;
253 : }
254 :
255 0 : if(!strncmp_w(rbuf->buf, "list "))
256 : {
257 0 : if((cp=strchr(rbuf->buf, ':')))
258 : {
259 0 : *cp='\0';
260 0 : if(!(listregex=strdup_w(cp+1, __func__)))
261 : goto end;
262 : }
263 0 : if(!(backupno=strdup_w(rbuf->buf+strlen("list "), __func__)))
264 : goto end;
265 :
266 : }
267 0 : else if(!strncmp_w(rbuf->buf, "listb "))
268 : {
269 0 : if((cp=strchr(rbuf->buf, ':')))
270 : {
271 0 : *cp='\0';
272 0 : if(!(browsedir=strdup_w(cp+1, __func__)))
273 : goto end;
274 : }
275 0 : strip_trailing_slashes(&browsedir);
276 0 : if(!(backupno=strdup_w(rbuf->buf+strlen("listb "), __func__)))
277 : goto end;
278 : }
279 0 : if(asfd->write_str(asfd, CMD_GEN, "ok")) goto end;
280 :
281 0 : iobuf_free_content(asfd->rbuf);
282 :
283 0 : if(list_server_init(asfd, sdirs, get_cntr(cconfs),
284 0 : get_protocol(cconfs), backupno, listregex, browsedir))
285 : goto end;
286 0 : ret=do_list_server();
287 : end:
288 0 : free_w(&backupno);
289 0 : free_w(&browsedir);
290 0 : free_w(&listregex);
291 0 : list_server_free();
292 0 : return ret;
293 : }
294 :
295 0 : static int run_diff(struct asfd *asfd,
296 : struct sdirs *sdirs, struct conf **cconfs)
297 : {
298 0 : int ret=-1;
299 0 : char *backup1=NULL;
300 0 : char *backup2=NULL;
301 0 : struct iobuf *rbuf=asfd->rbuf;
302 :
303 0 : if(!client_can_generic(cconfs, OPT_CLIENT_CAN_DIFF))
304 : {
305 : logp("Not allowing diff of %s\n",
306 0 : get_string(cconfs[OPT_CNAME]));
307 0 : asfd->write_str(asfd, CMD_GEN, "Client diff is not allowed");
308 0 : goto end;
309 : }
310 :
311 0 : if(!strncmp_w(rbuf->buf, "diff "))
312 : {
313 : char *cp;
314 0 : if((cp=strchr(rbuf->buf, ':')))
315 : {
316 0 : *cp='\0';
317 0 : if(!(backup2=strdup_w(cp+1, __func__)))
318 : goto end;
319 : }
320 0 : if(!(backup1=strdup_w(rbuf->buf+strlen("diff "), __func__)))
321 : goto end;
322 : }
323 0 : if(asfd->write_str(asfd, CMD_GEN, "ok")) goto end;
324 :
325 0 : iobuf_free_content(asfd->rbuf);
326 :
327 : ret=do_diff_server(asfd, sdirs,
328 0 : get_cntr(cconfs), get_protocol(cconfs), backup1, backup2);
329 : end:
330 0 : return ret;
331 : }
332 :
333 0 : static int unknown_command(struct asfd *asfd)
334 : {
335 0 : iobuf_log_unexpected(asfd->rbuf, __func__);
336 0 : asfd->write_str(asfd, CMD_ERROR, "unknown command");
337 0 : return -1;
338 : }
339 :
340 0 : static const char *buf_to_notify_str(struct iobuf *rbuf)
341 : {
342 0 : const char *buf=rbuf->buf;
343 0 : if(!strncmp_w(buf, "backup")) return "backup";
344 0 : else if(!strncmp_w(buf, "restore")) return "restore";
345 0 : else if(!strncmp_w(buf, "verify")) return "verify";
346 0 : else if(!strncmp_w(buf, "delete")) return "delete";
347 0 : else if(!strncmp_w(buf, "list")) return "list";
348 : else return "unknown";
349 : }
350 :
351 0 : static int run_action_server_do(struct async *as, struct sdirs *sdirs,
352 : const char *incexc, int srestore, int *timer_ret, struct conf **cconfs)
353 : {
354 : int ret;
355 0 : int resume=0;
356 0 : char msg[256]="";
357 0 : struct iobuf *rbuf=as->asfd->rbuf;
358 :
359 : // Make sure some directories exist.
360 0 : if(mkpath(&sdirs->current, sdirs->dedup))
361 : {
362 : snprintf(msg, sizeof(msg),
363 0 : "could not mkpath %s", sdirs->current);
364 0 : log_and_send(as->asfd, msg);
365 0 : return -1;
366 : }
367 :
368 0 : if(rbuf->cmd!=CMD_GEN) return unknown_command(as->asfd);
369 :
370 : // List and diff should work even while backups are running.
371 0 : if(!strncmp_w(rbuf->buf, "list ")
372 0 : || !strncmp_w(rbuf->buf, "listb "))
373 0 : return run_list(as->asfd, sdirs, cconfs);
374 :
375 0 : if(!strncmp_w(rbuf->buf, "diff "))
376 0 : return run_diff(as->asfd, sdirs, cconfs);
377 :
378 0 : switch((ret=get_lock_sdirs(as->asfd, sdirs)))
379 : {
380 : case 0: break; // OK.
381 : case 1: return 1; // Locked out.
382 : default: // Error.
383 : maybe_do_notification(as->asfd, ret,
384 : "", "error in get_lock_sdirs()",
385 0 : "", buf_to_notify_str(rbuf), cconfs);
386 0 : return -1;
387 : }
388 :
389 0 : switch((ret=check_for_rubble(as, sdirs, incexc, &resume, cconfs)))
390 : {
391 : case 0: break; // OK.
392 : case 1: return 1; // Now finalising.
393 : default: // Error.
394 : maybe_do_notification(as->asfd, ret,
395 : "", "error in check_for_rubble()",
396 0 : "", buf_to_notify_str(rbuf), cconfs);
397 0 : return -1;
398 : }
399 :
400 0 : if(!strncmp_w(rbuf->buf, "backup"))
401 : {
402 0 : ret=run_backup(as, sdirs, cconfs, incexc, timer_ret, resume);
403 0 : if(*timer_ret<0)
404 : maybe_do_notification(as->asfd, ret, "",
405 : "error running timer script",
406 0 : "", "backup", cconfs);
407 0 : else if(!*timer_ret)
408 : maybe_do_notification(as->asfd, ret, sdirs->client,
409 0 : sdirs->current, "log", "backup", cconfs);
410 0 : return ret;
411 : }
412 :
413 0 : if(!strncmp_w(rbuf->buf, "restore ")
414 0 : || !strncmp_w(rbuf->buf, "verify "))
415 0 : return run_restore(as->asfd, sdirs, cconfs, srestore);
416 :
417 0 : if(!strncmp_w(rbuf->buf, "Delete "))
418 0 : return run_delete(as->asfd, sdirs, cconfs);
419 :
420 : // Old clients will send 'delete', possibly accidentally due to the
421 : // user trying to use the new diff/long diff options.
422 : // Stop them from working, just to be safe.
423 0 : if(!strncmp_w(rbuf->buf, "delete "))
424 : {
425 : logp("old style delete from %s denied\n",
426 0 : get_string(cconfs[OPT_CNAME]));
427 : as->asfd->write_str(as->asfd, CMD_ERROR,
428 0 : "old style delete is not supported on this server");
429 0 : return -1;
430 : }
431 :
432 0 : return unknown_command(as->asfd);
433 : }
434 :
435 0 : int run_action_server(struct async *as,
436 : const char *incexc, int srestore, int *timer_ret, struct conf **cconfs)
437 : {
438 0 : int ret=-1;
439 0 : struct sdirs *sdirs=NULL;
440 0 : if((sdirs=sdirs_alloc())
441 0 : && !sdirs_init_from_confs(sdirs, cconfs))
442 : ret=run_action_server_do(as,
443 0 : sdirs, incexc, srestore, timer_ret, cconfs);
444 0 : if(sdirs) lock_release(sdirs->lock);
445 0 : sdirs_free(&sdirs);
446 0 : return ret;
447 : }
|