Line data Source code
1 : #include "burp.h"
2 : #include "base64.h"
3 : #include "cmd.h"
4 : #include "conf.h"
5 : #include "conffile.h"
6 : #include "client/main.h"
7 : #include "handy.h"
8 : #include "hexmap.h"
9 : #include "lock.h"
10 : #include "log.h"
11 : #include "strlist.h"
12 : #include "server/main.h"
13 : #include "server/protocol1/bedup.h"
14 : #include "server/protocol2/bsigs.h"
15 : #include "server/protocol2/bsparse.h"
16 : #include "server/protocol2/champ_chooser/champ_server.h"
17 :
18 0 : static void usage_server(void)
19 : {
20 : #ifndef HAVE_WIN32
21 0 : printf("\nThe configuration file specifies whether %s runs in server or client mode.\n", PACKAGE_TARNAME);
22 0 : printf("\nServer usage: %s [options]\n", progname());
23 0 : printf("\n");
24 0 : printf(" Options:\n");
25 0 : printf(" -a c Run as a stand-alone champion chooser.\n");
26 0 : printf(" -c <path> Path to conf file (default: %s).\n", config_default_path());
27 0 : printf(" -d <path> a single client in the status monitor.\n");
28 0 : printf(" -o <option> Override a given configuration option (you can use this flag several times).\n");
29 0 : printf(" -F Stay in the foreground.\n");
30 0 : printf(" -g Generate initial CA certificates and exit.\n");
31 0 : printf(" -h|-? Print this text and exit.\n");
32 0 : printf(" -i Print index of symbols and exit.\n");
33 0 : printf(" -n Do not fork any children (implies '-F').\n");
34 0 : printf(" -Q Do not log to stdout (overrides config file)\n");
35 0 : printf(" -t Dry-run to test config file syntax.\n");
36 0 : printf(" -v Print version and exit.\n");
37 0 : printf("Options to use with '-a c':\n");
38 0 : printf(" -C <client> Run as if forked via a connection from this client.\n");
39 0 : printf("\n");
40 : #endif
41 0 : }
42 :
43 0 : static void usage_client(void)
44 : {
45 0 : printf("\nClient usage: %s [options]\n", progname());
46 0 : printf("\n");
47 0 : printf(" Options:\n");
48 0 : printf(" -a <action> The action can be one of the following.\n");
49 0 : printf(" b: backup\n");
50 0 : printf(" delete: delete\n");
51 0 : printf(" d: diff\n");
52 0 : printf(" e: estimate\n");
53 0 : printf(" l: list (this is the default when an action is not given)\n");
54 0 : printf(" L: long list\n");
55 0 : printf(" m: monitor interface\n");
56 0 : printf(" r: restore\n");
57 : #ifndef HAVE_WIN32
58 0 : printf(" s: status monitor (ncurses)\n");
59 0 : printf(" S: status monitor snapshot\n");
60 : #endif
61 0 : printf(" t: timed backup\n");
62 0 : printf(" T: check backup timer, but do not actually backup\n");
63 0 : printf(" v: verify\n");
64 0 : printf(" -b <number> Backup number (default: the most recent backup).\n");
65 0 : printf(" -c <path> Path to conf file (default: %s).\n", config_default_path());
66 0 : printf(" -d <directory> Directory to restore to, or directory to list.\n");
67 0 : printf(" -o <option> Override a given configuration option (you can use this flag several times).\n");
68 0 : printf(" -f Allow overwrite during restore.\n");
69 0 : printf(" -h|-? Print this text and exit.\n");
70 0 : printf(" -i Print index of symbols and exit.\n");
71 0 : printf(" -q <max secs> Randomised delay of starting a timed backup.\n");
72 0 : printf(" -Q Do not log to stdout (overrides config file)\n");
73 0 : printf(" -r <regex> Specify a regular expression.\n");
74 0 : printf(" -s <number> Number of leading path components to strip during restore.\n");
75 0 : printf(" -t Dry-run to test config file syntax.\n");
76 0 : printf(" -v Print version and exit.\n");
77 : #ifdef HAVE_WIN32
78 : printf(" -x Do not use the Windows VSS API when restoring.\n");
79 : #else
80 0 : printf(" -x Strip Windows VSS data when restoring.\n");
81 0 : printf("Options to use with '-a S':\n");
82 0 : printf(" -C <client> Show a particular client.\n");
83 0 : printf(" -b <number> Show listable files in a particular backup (requires -C).\n");
84 0 : printf(" -d <path> Show a particular path in a backup (requires -C and -b).\n");
85 0 : printf(" -l <path> Log file for the status monitor.\n");
86 0 : printf(" -z <file> Dump a particular log file in a backup (requires -C and -b).\n");
87 : #endif
88 0 : printf("\n");
89 : #ifndef HAVE_WIN32
90 0 : printf(" See %s or the man page ('man %s') for usage examples\n",
91 : PACKAGE_URL, PACKAGE_TARNAME);
92 0 : printf(" and additional configuration options.\n\n");
93 : #else
94 : printf(" See %s for usage examples and additional configuration\n",
95 : PACKAGE_TARNAME);
96 : printf(" options.\n\n");
97 : #endif
98 0 : }
99 :
100 0 : int reload(struct conf **confs, const char *conffile, bool firsttime)
101 : {
102 0 : if(!firsttime) logp("Reloading config\n");
103 :
104 0 : if(confs_init(confs)) return -1;
105 :
106 0 : if(conf_load_global_only(conffile, confs)) return -1;
107 :
108 0 : umask(get_mode_t(confs[OPT_UMASK]));
109 :
110 : // This will turn on syslogging which could not be turned on before
111 : // conf_load.
112 0 : log_fzp_set(NULL, confs);
113 :
114 : #ifndef HAVE_WIN32
115 0 : if(get_e_burp_mode(confs[OPT_BURP_MODE])==BURP_MODE_SERVER)
116 0 : setup_signals();
117 : #endif
118 :
119 : return 0;
120 : }
121 :
122 : static int replace_conf_str(struct conf *conf, const char *newval)
123 : {
124 0 : if(!newval) return 0;
125 0 : return set_string(conf, newval);
126 : }
127 :
128 : static void usage(void)
129 : {
130 0 : usage_server();
131 0 : usage_client();
132 : }
133 :
134 0 : static int parse_action(enum action *act, const char *optarg)
135 : {
136 0 : if(!strncmp(optarg, "backup", 1))
137 0 : *act=ACTION_BACKUP;
138 0 : else if(!strncmp(optarg, "timedbackup", 1))
139 0 : *act=ACTION_BACKUP_TIMED;
140 0 : else if(!strncmp(optarg, "Timercheck", 1))
141 0 : *act=ACTION_TIMER_CHECK;
142 0 : else if(!strncmp(optarg, "restore", 1))
143 0 : *act=ACTION_RESTORE;
144 0 : else if(!strncmp(optarg, "verify", 1))
145 0 : *act=ACTION_VERIFY;
146 0 : else if(!strncmp(optarg, "list", 1))
147 0 : *act=ACTION_LIST;
148 0 : else if(!strncmp(optarg, "List", 1))
149 0 : *act=ACTION_LIST_LONG;
150 0 : else if(!strncmp(optarg, "status", 1))
151 0 : *act=ACTION_STATUS;
152 0 : else if(!strncmp(optarg, "Status", 1))
153 0 : *act=ACTION_STATUS_SNAPSHOT;
154 0 : else if(!strncmp(optarg, "estimate", 1))
155 0 : *act=ACTION_ESTIMATE;
156 : // Make them spell 'delete' out fully so that it is less likely to be
157 : // used accidently.
158 0 : else if(!strncmp_w(optarg, "delete"))
159 0 : *act=ACTION_DELETE;
160 0 : else if(!strncmp(optarg, "champchooser", 1))
161 0 : *act=ACTION_CHAMP_CHOOSER;
162 0 : else if(!strncmp(optarg, "diff", 1))
163 0 : *act=ACTION_DIFF;
164 0 : else if(!strncmp(optarg, "Diff", 1))
165 0 : *act=ACTION_DIFF_LONG;
166 0 : else if(!strncmp(optarg, "monitor", 1))
167 0 : *act=ACTION_MONITOR;
168 : else
169 : {
170 : usage();
171 0 : return -1;
172 : }
173 : return 0;
174 : }
175 :
176 : #ifndef HAVE_WIN32
177 0 : static int run_champ_chooser(struct conf **confs)
178 : {
179 0 : const char *orig_client=get_string(confs[OPT_ORIG_CLIENT]);
180 0 : if(orig_client && *orig_client)
181 0 : return champ_chooser_server_standalone(confs);
182 0 : logp("No client name given for standalone champion chooser process.\n");
183 0 : logp("Try using the '-C' option.\n");
184 0 : return 1;
185 : }
186 :
187 0 : static int server_modes(enum action act,
188 : const char *conffile, struct lock *lock, int generate_ca_only,
189 : struct conf **confs)
190 : {
191 0 : switch(act)
192 : {
193 : case ACTION_CHAMP_CHOOSER:
194 : // We are running on the server machine, wanting to
195 : // be a standalone champion chooser process.
196 0 : return run_champ_chooser(confs);
197 : default:
198 0 : return server(confs, conffile, lock, generate_ca_only);
199 : }
200 : }
201 : #endif
202 :
203 0 : static void random_delay(struct conf **confs)
204 : {
205 : int delay;
206 0 : int randomise=get_int(confs[OPT_RANDOMISE]);
207 0 : if(!randomise) return;
208 0 : srand(getpid());
209 0 : delay=rand()%randomise;
210 0 : logp("Sleeping %d seconds\n", delay);
211 0 : sleep(delay);
212 : }
213 :
214 0 : static int run_test_confs(struct conf **confs, const char *client)
215 : {
216 0 : int ret=-1;
217 0 : struct conf **cconfs=NULL;
218 0 : if(!client)
219 : {
220 0 : confs_dump(confs, 0);
221 0 : ret=0;
222 0 : goto end;
223 : }
224 0 : if(!(cconfs=confs_alloc()))
225 : goto end;
226 0 : confs_init(cconfs);
227 0 : if(set_string(cconfs[OPT_CNAME], client)
228 0 : || set_string(cconfs[OPT_PEER_VERSION], PACKAGE_VERSION)
229 0 : || conf_load_clientconfdir(confs, cconfs))
230 : goto end;
231 0 : confs_dump(cconfs, CONF_FLAG_CC_OVERRIDE|CONF_FLAG_INCEXC);
232 :
233 : end:
234 0 : confs_free(&cconfs);
235 0 : return ret;
236 : }
237 :
238 0 : static struct lock *get_prog_lock(struct conf **confs)
239 : {
240 0 : struct lock *lock=NULL;
241 0 : const char *lockfile=confs_get_lockfile(confs);
242 0 : if(!(lock=lock_alloc_and_init(lockfile)))
243 : goto error;
244 0 : lock_get(lock);
245 0 : switch(lock->status)
246 : {
247 : case GET_LOCK_GOT:
248 : return lock;
249 : case GET_LOCK_NOT_GOT:
250 0 : logp("Could not get lockfile.\n");
251 0 : logp("Another process is probably running.\n");
252 0 : goto error;
253 : case GET_LOCK_ERROR:
254 : default:
255 0 : logp("Could not get lockfile.\n");
256 0 : logp("Maybe you do not have permissions to write to %s.\n", lockfile);
257 0 : goto error;
258 : }
259 : error:
260 0 : lock_free(&lock);
261 0 : return NULL;
262 : }
263 :
264 : #ifdef HAVE_WIN32
265 : #define main RealMain
266 : #endif
267 : #ifndef UTEST
268 : static
269 : #endif
270 0 : int real_main(int argc, char *argv[])
271 : {
272 0 : int ret=1;
273 0 : int option=0;
274 0 : int daemon=1;
275 0 : int forking=1;
276 0 : int strip=0;
277 0 : int randomise=0;
278 0 : struct lock *lock=NULL;
279 0 : struct conf **confs=NULL;
280 0 : int forceoverwrite=0;
281 0 : enum action act=ACTION_LIST;
282 0 : const char *backup=NULL;
283 0 : const char *backup2=NULL;
284 0 : char *restoreprefix=NULL;
285 0 : char *stripfrompath=NULL;
286 0 : const char *regex=NULL;
287 0 : const char *browsefile=NULL;
288 0 : char *browsedir=NULL;
289 0 : const char *conffile=config_default_path();
290 0 : const char *orig_client=NULL;
291 0 : const char *logfile=NULL;
292 : // The orig_client is the original client that the normal client
293 : // would like to restore from.
294 : #ifndef HAVE_WIN32
295 0 : int generate_ca_only=0;
296 : #endif
297 0 : int vss_restore=1;
298 0 : int test_confs=0;
299 : enum burp_mode mode;
300 0 : struct strlist *cli_overrides=NULL;
301 :
302 0 : log_init(argv[0]);
303 : #ifndef HAVE_WIN32
304 0 : if(!strcmp(prog, "bedup"))
305 0 : return run_bedup(argc, argv);
306 0 : if(!strcmp(prog, "bsigs"))
307 0 : return run_bsigs(argc, argv);
308 0 : if(!strcmp(prog, "bsparse"))
309 0 : return run_bsparse(argc, argv);
310 : #endif
311 :
312 0 : while((option=getopt(argc, argv, "a:b:c:C:d:o:fFghil:nq:Qr:s:tvxjz:?"))!=-1)
313 : {
314 0 : switch(option)
315 : {
316 : case 'a':
317 0 : if(parse_action(&act, optarg)) goto end;
318 : break;
319 : case 'b':
320 : // The diff command may have two backups
321 : // specified.
322 0 : if(!backup2 && backup) backup2=optarg;
323 0 : if(!backup) backup=optarg;
324 : break;
325 : case 'c':
326 0 : conffile=optarg;
327 0 : break;
328 : case 'C':
329 0 : orig_client=optarg;
330 0 : break;
331 : case 'd':
332 0 : restoreprefix=optarg; // for restores
333 0 : browsedir=optarg; // for lists
334 0 : break;
335 : case 'f':
336 : forceoverwrite=1;
337 : break;
338 : case 'F':
339 0 : daemon=0;
340 0 : break;
341 : case 'g':
342 : #ifndef HAVE_WIN32
343 0 : generate_ca_only=1;
344 : #endif
345 0 : break;
346 : case 'i':
347 0 : cmd_print_all();
348 0 : ret=0;
349 0 : goto end;
350 : case 'l':
351 0 : logfile=optarg;
352 0 : break;
353 : case 'n':
354 0 : forking=0;
355 0 : break;
356 : case 'o':
357 0 : strlist_add(&cli_overrides, optarg, 0);
358 0 : break;
359 : case 'q':
360 0 : randomise=atoi(optarg);
361 0 : break;
362 : case 'Q':
363 0 : log_force_quiet();
364 0 : break;
365 : case 'r':
366 0 : regex=optarg;
367 0 : break;
368 : case 's':
369 0 : strip=atoi(optarg);
370 0 : break;
371 : case 'v':
372 0 : printf("%s-%s\n", progname(), PACKAGE_VERSION);
373 0 : ret=0;
374 0 : goto end;
375 : case 'x':
376 0 : vss_restore=0;
377 0 : break;
378 : case 't':
379 0 : test_confs=1;
380 0 : break;
381 : case 'z':
382 0 : browsefile=optarg;
383 0 : break;
384 : case 'h':
385 : case '?':
386 : default:
387 : usage();
388 : goto end;
389 : }
390 : }
391 0 : if(optind<argc)
392 : {
393 : usage();
394 : goto end;
395 : }
396 :
397 0 : if(act==ACTION_MONITOR)
398 : {
399 : // Try to output everything in JSON.
400 0 : log_set_json(1);
401 : // Need to do this so that processes reading stdout get the
402 : // result of the printfs of logp straight away.
403 0 : setvbuf(stdout, NULL, _IONBF, 0);
404 : }
405 :
406 0 : conf_set_cli_overrides(cli_overrides);
407 0 : if(!(confs=confs_alloc()))
408 : goto end;
409 :
410 0 : if(reload(confs, conffile, 1))
411 : goto end;
412 :
413 : // Dry run to test config file syntax.
414 0 : if(test_confs)
415 : {
416 0 : ret=run_test_confs(confs, orig_client);
417 0 : goto end;
418 : }
419 :
420 0 : if(!backup) switch(act)
421 : {
422 : case ACTION_DELETE:
423 0 : logp("No backup specified for deletion.\n");
424 0 : goto end;
425 : case ACTION_RESTORE:
426 : case ACTION_VERIFY:
427 : case ACTION_DIFF:
428 : case ACTION_DIFF_LONG:
429 0 : logp("No backup specified. Using the most recent.\n");
430 0 : backup="0";
431 : default:
432 : break;
433 : }
434 0 : if(!backup2) switch(act)
435 : {
436 : case ACTION_DIFF:
437 : case ACTION_DIFF_LONG:
438 0 : logp("No second backup specified. Using file system scan.\n");
439 0 : backup2="n"; // For 'next'.
440 : default:
441 : break;
442 : }
443 :
444 : // The logfile option is only used for the status client stuff.
445 0 : if(logfile
446 0 : && (act!=ACTION_STATUS
447 0 : && act!=ACTION_STATUS_SNAPSHOT))
448 0 : logp("-l <logfile> option obsoleted\n");
449 :
450 0 : if(orig_client
451 0 : && *orig_client
452 0 : && set_string(confs[OPT_ORIG_CLIENT], orig_client))
453 : goto end;
454 :
455 : // The random delay needs to happen before the lock is got, otherwise
456 : // you would never be able to use burp by hand.
457 0 : if(randomise) set_int(confs[OPT_RANDOMISE], randomise);
458 0 : mode=get_e_burp_mode(confs[OPT_BURP_MODE]);
459 0 : if(mode==BURP_MODE_CLIENT
460 0 : && (act==ACTION_BACKUP_TIMED || act==ACTION_TIMER_CHECK))
461 0 : random_delay(confs);
462 :
463 0 : if(mode==BURP_MODE_SERVER)
464 : {
465 0 : switch(act)
466 : {
467 : case ACTION_CHAMP_CHOOSER:
468 : // Need to run without getting the lock.
469 : break;
470 : default:
471 0 : if(!(lock=get_prog_lock(confs)))
472 : goto end;
473 : break;
474 : }
475 : }
476 0 : else if(mode==BURP_MODE_CLIENT)
477 : {
478 0 : switch(act)
479 : {
480 : case ACTION_BACKUP:
481 : case ACTION_BACKUP_TIMED:
482 : case ACTION_TIMER_CHECK:
483 : // Need to get the lock.
484 0 : if(!(lock=get_prog_lock(confs)))
485 : goto end;
486 : break;
487 : default:
488 : break;
489 : }
490 : }
491 :
492 : // Change privileges after having got the lock, for convenience.
493 0 : if(chuser_and_or_chgrp(
494 0 : get_string(confs[OPT_USER]), get_string(confs[OPT_GROUP])))
495 : return -1;
496 :
497 0 : set_int(confs[OPT_OVERWRITE], forceoverwrite);
498 0 : set_int(confs[OPT_STRIP], strip);
499 0 : set_int(confs[OPT_FORK], forking);
500 0 : set_int(confs[OPT_DAEMON], daemon);
501 :
502 0 : strip_trailing_slashes(&restoreprefix);
503 0 : strip_trailing_slashes(&browsedir);
504 0 : if(replace_conf_str(confs[OPT_BACKUP], backup)
505 0 : || replace_conf_str(confs[OPT_BACKUP2], backup2)
506 0 : || replace_conf_str(confs[OPT_RESTOREPREFIX], restoreprefix)
507 0 : || replace_conf_str(confs[OPT_STRIP_FROM_PATH], stripfrompath)
508 0 : || replace_conf_str(confs[OPT_REGEX], regex)
509 0 : || replace_conf_str(confs[OPT_BROWSEFILE], browsefile)
510 0 : || replace_conf_str(confs[OPT_BROWSEDIR], browsedir)
511 0 : || replace_conf_str(confs[OPT_MONITOR_LOGFILE], logfile))
512 : goto end;
513 :
514 0 : base64_init();
515 0 : hexmap_init();
516 :
517 0 : if(mode==BURP_MODE_SERVER)
518 : {
519 : #ifdef HAVE_WIN32
520 : logp("Sorry, server mode is not implemented for Windows.\n");
521 : #else
522 0 : ret=server_modes(act,
523 : conffile, lock, generate_ca_only, confs);
524 : #endif
525 : }
526 : else
527 : {
528 0 : ret=client(confs, act, vss_restore);
529 : }
530 :
531 : end:
532 0 : lock_release(lock);
533 0 : lock_free(&lock);
534 0 : confs_free(&confs);
535 0 : strlists_free(&cli_overrides);
536 0 : return ret;
537 : }
538 :
539 : #ifndef UTEST
540 : int main(int argc, char *argv[])
541 : {
542 : return real_main(argc, argv);
543 : }
544 : #endif
|