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