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

Generated by: LCOV version 1.13