LCOV - code coverage report
Current view: top level - src - prog.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 0 230 0.0 %
Date: 2017-07-01 Functions: 0 10 0.0 %

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

Generated by: LCOV version 1.10