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(set_string(cconfs[OPT_BACKUP], backupnostr))
170 : goto end;
171 : // FIX THIS.
172 0 : if((cp=strchr(cconfs[OPT_BACKUP]->data.s, ':'))) *cp='\0';
173 :
174 0 : if(act==ACTION_RESTORE)
175 : {
176 : int r;
177 0 : if((r=client_can_restore(cconfs))<0)
178 : goto end;
179 0 : else if(!r)
180 : {
181 0 : logp("Not allowing restore of %s\n", cname);
182 0 : if(!asfd->write_str(asfd, CMD_GEN,
183 0 : "Client restore is not allowed")) ret=0;
184 : goto end;
185 : }
186 : }
187 0 : if(act==ACTION_VERIFY
188 0 : && !(client_can_generic(cconfs, OPT_CLIENT_CAN_VERIFY)))
189 : {
190 0 : logp("Not allowing verify of %s\n", cname);
191 0 : if(!asfd->write_str(asfd, CMD_GEN,
192 0 : "Client verify is not allowed")) ret=0;
193 : goto end;
194 : }
195 :
196 0 : if((restoreregex=strchr(copy, ':')))
197 : {
198 0 : *restoreregex='\0';
199 0 : restoreregex++;
200 : }
201 0 : if(restoreregex && *restoreregex
202 0 : && set_string(cconfs[OPT_REGEX], restoreregex))
203 : goto end;
204 0 : if(asfd->write_str(asfd, CMD_GEN, "ok"))
205 : goto end;
206 : ret=do_restore_server(asfd, sdirs, act,
207 0 : srestore, &dir_for_notify, cconfs);
208 0 : if(dir_for_notify)
209 : maybe_do_notification(asfd, ret,
210 : sdirs->client, dir_for_notify,
211 : act==ACTION_RESTORE?"restorelog":"verifylog",
212 : act==ACTION_RESTORE?"restore":"verify",
213 0 : cconfs);
214 : end:
215 0 : free_w(©);
216 0 : free_w(&dir_for_notify);
217 0 : return ret;
218 : }
219 :
220 0 : static int run_delete(struct asfd *asfd,
221 : struct sdirs *sdirs, struct conf **cconfs)
222 : {
223 0 : char *backupno=NULL;
224 0 : struct iobuf *rbuf=asfd->rbuf;
225 0 : const char *cname=get_string(cconfs[OPT_CNAME]);
226 0 : if(!client_can_generic(cconfs, OPT_CLIENT_CAN_DELETE))
227 : {
228 0 : logp("Not allowing delete of %s\n", cname);
229 0 : asfd->write_str(asfd, CMD_GEN, "Client delete is not allowed");
230 0 : return -1;
231 : }
232 0 : backupno=rbuf->buf+strlen("delete ");
233 : return do_delete_server(asfd, sdirs,
234 : get_cntr(cconfs), cname, backupno,
235 0 : get_string(cconfs[OPT_MANUAL_DELETE]));
236 : }
237 :
238 0 : static int run_list(struct asfd *asfd,
239 : struct sdirs *sdirs, struct conf **cconfs)
240 : {
241 0 : int ret=-1;
242 0 : char *cp=NULL;
243 0 : char *backupno=NULL;
244 0 : char *browsedir=NULL;
245 0 : char *listregex=NULL;
246 0 : struct iobuf *rbuf=asfd->rbuf;
247 :
248 0 : if(!client_can_generic(cconfs, OPT_CLIENT_CAN_LIST))
249 : {
250 : logp("Not allowing list of %s\n",
251 0 : get_string(cconfs[OPT_CNAME]));
252 0 : asfd->write_str(asfd, CMD_GEN, "Client list is not allowed");
253 0 : goto end;
254 : }
255 :
256 0 : if(!strncmp_w(rbuf->buf, "list "))
257 : {
258 0 : if((cp=strchr(rbuf->buf, ':')))
259 : {
260 0 : *cp='\0';
261 0 : if(!(listregex=strdup_w(cp+1, __func__)))
262 : goto end;
263 : }
264 0 : if(!(backupno=strdup_w(rbuf->buf+strlen("list "), __func__)))
265 : goto end;
266 :
267 : }
268 0 : else if(!strncmp_w(rbuf->buf, "listb "))
269 : {
270 0 : if((cp=strchr(rbuf->buf, ':')))
271 : {
272 0 : *cp='\0';
273 0 : if(!(browsedir=strdup_w(cp+1, __func__)))
274 : goto end;
275 : }
276 0 : strip_trailing_slashes(&browsedir);
277 0 : if(!(backupno=strdup_w(rbuf->buf+strlen("listb "), __func__)))
278 : goto end;
279 : }
280 0 : if(asfd->write_str(asfd, CMD_GEN, "ok")) goto end;
281 :
282 0 : iobuf_free_content(asfd->rbuf);
283 :
284 0 : if(list_server_init(asfd, sdirs, get_cntr(cconfs),
285 0 : get_protocol(cconfs), backupno, listregex, browsedir))
286 : goto end;
287 0 : ret=do_list_server();
288 : end:
289 0 : free_w(&backupno);
290 0 : free_w(&browsedir);
291 0 : free_w(&listregex);
292 0 : list_server_free();
293 0 : return ret;
294 : }
295 :
296 0 : static int run_diff(struct asfd *asfd,
297 : struct sdirs *sdirs, struct conf **cconfs)
298 : {
299 0 : int ret=-1;
300 0 : char *backup1=NULL;
301 0 : char *backup2=NULL;
302 0 : struct iobuf *rbuf=asfd->rbuf;
303 :
304 0 : if(!client_can_generic(cconfs, OPT_CLIENT_CAN_DIFF))
305 : {
306 : logp("Not allowing diff of %s\n",
307 0 : get_string(cconfs[OPT_CNAME]));
308 0 : asfd->write_str(asfd, CMD_GEN, "Client diff is not allowed");
309 0 : goto end;
310 : }
311 :
312 0 : if(!strncmp_w(rbuf->buf, "diff "))
313 : {
314 : char *cp;
315 0 : if((cp=strchr(rbuf->buf, ':')))
316 : {
317 0 : *cp='\0';
318 0 : if(!(backup2=strdup_w(cp+1, __func__)))
319 : goto end;
320 : }
321 0 : if(!(backup1=strdup_w(rbuf->buf+strlen("diff "), __func__)))
322 : goto end;
323 : }
324 0 : if(asfd->write_str(asfd, CMD_GEN, "ok")) goto end;
325 :
326 0 : iobuf_free_content(asfd->rbuf);
327 :
328 : ret=do_diff_server(asfd, sdirs,
329 0 : get_cntr(cconfs), get_protocol(cconfs), backup1, backup2);
330 : end:
331 0 : return ret;
332 : }
333 :
334 0 : static int unknown_command(struct asfd *asfd)
335 : {
336 0 : iobuf_log_unexpected(asfd->rbuf, __func__);
337 0 : asfd->write_str(asfd, CMD_ERROR, "unknown command");
338 0 : return -1;
339 : }
340 :
341 0 : static const char *buf_to_notify_str(struct iobuf *rbuf)
342 : {
343 0 : const char *buf=rbuf->buf;
344 0 : if(!strncmp_w(buf, "backup")) return "backup";
345 0 : else if(!strncmp_w(buf, "restore")) return "restore";
346 0 : else if(!strncmp_w(buf, "verify")) return "verify";
347 0 : else if(!strncmp_w(buf, "delete")) return "delete";
348 0 : else if(!strncmp_w(buf, "list")) return "list";
349 : else return "unknown";
350 : }
351 :
352 0 : static int run_action_server_do(struct async *as, struct sdirs *sdirs,
353 : const char *incexc, int srestore, int *timer_ret, struct conf **cconfs)
354 : {
355 : int ret;
356 0 : int resume=0;
357 0 : char msg[256]="";
358 0 : struct iobuf *rbuf=as->asfd->rbuf;
359 :
360 : // Make sure some directories exist.
361 0 : if(mkpath(&sdirs->current, sdirs->dedup))
362 : {
363 : snprintf(msg, sizeof(msg),
364 0 : "could not mkpath %s", sdirs->current);
365 0 : log_and_send(as->asfd, msg);
366 0 : return -1;
367 : }
368 :
369 0 : if(rbuf->cmd!=CMD_GEN) return unknown_command(as->asfd);
370 :
371 : // List and diff should work even while backups are running.
372 0 : if(!strncmp_w(rbuf->buf, "list ")
373 0 : || !strncmp_w(rbuf->buf, "listb "))
374 0 : return run_list(as->asfd, sdirs, cconfs);
375 :
376 0 : if(!strncmp_w(rbuf->buf, "diff "))
377 0 : return run_diff(as->asfd, sdirs, cconfs);
378 :
379 0 : switch((ret=get_lock_sdirs(as->asfd, sdirs)))
380 : {
381 : case 0: break; // OK.
382 : case 1: return 1; // Locked out.
383 : default: // Error.
384 : maybe_do_notification(as->asfd, ret,
385 : "", "error in get_lock_sdirs()",
386 0 : "", buf_to_notify_str(rbuf), cconfs);
387 0 : return -1;
388 : }
389 :
390 0 : switch((ret=check_for_rubble(as, sdirs, incexc, &resume, cconfs)))
391 : {
392 : case 0: break; // OK.
393 : case 1: return 1; // Now finalising.
394 : default: // Error.
395 : maybe_do_notification(as->asfd, ret,
396 : "", "error in check_for_rubble()",
397 0 : "", buf_to_notify_str(rbuf), cconfs);
398 0 : return -1;
399 : }
400 :
401 0 : if(!strncmp_w(rbuf->buf, "backup"))
402 : {
403 0 : ret=run_backup(as, sdirs, cconfs, incexc, timer_ret, resume);
404 0 : if(*timer_ret<0)
405 : maybe_do_notification(as->asfd, ret, "",
406 : "error running timer script",
407 0 : "", "backup", cconfs);
408 0 : else if(!*timer_ret)
409 : maybe_do_notification(as->asfd, ret, sdirs->client,
410 0 : sdirs->current, "log", "backup", cconfs);
411 0 : return ret;
412 : }
413 :
414 0 : if(!strncmp_w(rbuf->buf, "restore ")
415 0 : || !strncmp_w(rbuf->buf, "verify "))
416 0 : return run_restore(as->asfd, sdirs, cconfs, srestore);
417 :
418 0 : if(!strncmp_w(rbuf->buf, "Delete "))
419 0 : return run_delete(as->asfd, sdirs, cconfs);
420 :
421 : // Old clients will send 'delete', possibly accidentally due to the
422 : // user trying to use the new diff/long diff options.
423 : // Stop them from working, just to be safe.
424 0 : if(!strncmp_w(rbuf->buf, "delete "))
425 : {
426 : logp("old style delete from %s denied\n",
427 0 : get_string(cconfs[OPT_CNAME]));
428 : as->asfd->write_str(as->asfd, CMD_ERROR,
429 0 : "old style delete is not supported on this server");
430 0 : return -1;
431 : }
432 :
433 0 : return unknown_command(as->asfd);
434 : }
435 :
436 0 : int run_action_server(struct async *as,
437 : const char *incexc, int srestore, int *timer_ret, struct conf **cconfs)
438 : {
439 0 : int ret=-1;
440 0 : struct sdirs *sdirs=NULL;
441 0 : if((sdirs=sdirs_alloc())
442 0 : && !sdirs_init_from_confs(sdirs, cconfs))
443 : ret=run_action_server_do(as,
444 0 : sdirs, incexc, srestore, timer_ret, cconfs);
445 0 : if(sdirs) lock_release(sdirs->lock);
446 0 : sdirs_free(&sdirs);
447 0 : return ret;
448 : }
|