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-08-09 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 %s runs in server or client mode.\n", PACKAGE_TARNAME);
      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 %s or the man page ('man %s') for usage examples\n",
      86             :                 PACKAGE_URL, PACKAGE_TARNAME);
      87             :         printf(" and additional configuration options.\n\n");
      88             : #else
      89             :         printf(" See %s for usage examples and additional configuration\n",
      90             :                 PACKAGE_TARNAME);
      91             :         printf(" options.\n\n");
      92             : #endif
      93           0 : }
      94             : 
      95           0 : int reload(struct conf **confs, const char *conffile, bool firsttime)
      96             : {
      97           0 :         if(!firsttime) logp("Reloading config\n");
      98             : 
      99           0 :         if(confs_init(confs)) return -1;
     100             : 
     101           0 :         if(conf_load_global_only(conffile, confs)) return -1;
     102             : 
     103           0 :         umask(get_mode_t(confs[OPT_UMASK]));
     104             : 
     105             :         // This will turn on syslogging which could not be turned on before
     106             :         // conf_load.
     107           0 :         log_fzp_set(NULL, confs);
     108             : 
     109             : #ifndef HAVE_WIN32
     110           0 :         if(get_e_burp_mode(confs[OPT_BURP_MODE])==BURP_MODE_SERVER)
     111           0 :                 setup_signals();
     112             : #endif
     113             : 
     114             :         // Do not try to change user or group after the first time.
     115           0 :         if(firsttime && chuser_and_or_chgrp(
     116           0 :                 get_string(confs[OPT_USER]), get_string(confs[OPT_GROUP])))
     117             :                         return -1;
     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             : 
     301           0 :         log_init(argv[0]);
     302             : #ifndef HAVE_WIN32
     303           0 :         if(!strcmp(prog, "bedup"))
     304           0 :                 return run_bedup(argc, argv);
     305           0 :         if(!strcmp(prog, "bsigs"))
     306           0 :                 return run_bsigs(argc, argv);
     307           0 :         if(!strcmp(prog, "bsparse"))
     308           0 :                 return run_bsparse(argc, argv);
     309             : #endif
     310             : 
     311           0 :         while((option=getopt(argc, argv, "a:b:c:C:d:fFghil:nq:Qr:s:tvxjz:?"))!=-1)
     312             :         {
     313           0 :                 switch(option)
     314             :                 {
     315             :                         case 'a':
     316           0 :                                 if(parse_action(&act, optarg)) goto end;
     317             :                                 break;
     318             :                         case 'b':
     319             :                                 // The diff command may have two backups
     320             :                                 // specified.
     321           0 :                                 if(!backup2 && backup) backup2=optarg;
     322           0 :                                 if(!backup) backup=optarg;
     323             :                                 break;
     324             :                         case 'c':
     325           0 :                                 conffile=optarg;
     326           0 :                                 break;
     327             :                         case 'C':
     328           0 :                                 orig_client=optarg;
     329           0 :                                 break;
     330             :                         case 'd':
     331           0 :                                 restoreprefix=optarg; // for restores
     332           0 :                                 browsedir=optarg; // for lists
     333           0 :                                 break;
     334             :                         case 'f':
     335             :                                 forceoverwrite=1;
     336             :                                 break;
     337             :                         case 'F':
     338           0 :                                 daemon=0;
     339           0 :                                 break;
     340             :                         case 'g':
     341             : #ifndef HAVE_WIN32
     342           0 :                                 generate_ca_only=1;
     343             : #endif
     344           0 :                                 break;
     345             :                         case 'i':
     346           0 :                                 cmd_print_all();
     347           0 :                                 ret=0;
     348           0 :                                 goto end;
     349             :                         case 'l':
     350           0 :                                 logfile=optarg;
     351           0 :                                 break;
     352             :                         case 'n':
     353           0 :                                 forking=0;
     354           0 :                                 break;
     355             :                         case 'q':
     356           0 :                                 randomise=atoi(optarg);
     357           0 :                                 break;
     358             :                         case 'Q':
     359           0 :                                 log_force_quiet();
     360           0 :                                 break;
     361             :                         case 'r':
     362           0 :                                 regex=optarg;
     363           0 :                                 break;
     364             :                         case 's':
     365           0 :                                 strip=atoi(optarg);
     366           0 :                                 break;
     367             :                         case 'v':
     368           0 :                                 printf("%s-%s\n", progname(), PACKAGE_VERSION);
     369           0 :                                 ret=0;
     370           0 :                                 goto end;
     371             :                         case 'x':
     372           0 :                                 vss_restore=0;
     373           0 :                                 break;
     374             :                         case 't':
     375           0 :                                 test_confs=1;
     376           0 :                                 break;
     377             :                         case 'z':
     378           0 :                                 browsefile=optarg;
     379           0 :                                 break;
     380             :                         case 'h':
     381             :                         case '?':
     382             :                         default:
     383             :                                 usage();
     384             :                                 goto end;
     385             :                 }
     386             :         }
     387           0 :         if(optind<argc)
     388             :         {
     389             :                 usage();
     390             :                 goto end;
     391             :         }
     392             : 
     393           0 :         if(act==ACTION_MONITOR)
     394             :         {
     395             :                 // Try to output everything in JSON.
     396           0 :                 log_set_json(1);
     397             : #ifndef HAVE_WIN32
     398             :                 // Need to do this so that processes reading stdout get the
     399             :                 // result of the printfs of logp straight away.
     400           0 :                 setlinebuf(stdout);
     401             : #endif
     402             :         }
     403             : 
     404           0 :         if(!(confs=confs_alloc()))
     405             :                 goto end;
     406             : 
     407           0 :         if(reload(confs, conffile, 1))
     408             :                 goto end;
     409             : 
     410             :         // Dry run to test config file syntax.
     411           0 :         if(test_confs)
     412             :         {
     413           0 :                 ret=run_test_confs(confs, orig_client);
     414           0 :                 goto end;
     415             :         }
     416             : 
     417           0 :         if(!backup) switch(act)
     418             :         {
     419             :                 case ACTION_DELETE:
     420           0 :                         logp("No backup specified for deletion.\n");
     421           0 :                         goto end;
     422             :                 case ACTION_RESTORE:
     423             :                 case ACTION_VERIFY:
     424             :                 case ACTION_DIFF:
     425             :                 case ACTION_DIFF_LONG:
     426           0 :                         logp("No backup specified. Using the most recent.\n");
     427           0 :                         backup="0";
     428             :                 default:
     429             :                         break;
     430             :         }
     431           0 :         if(!backup2) switch(act)
     432             :         {
     433             :                 case ACTION_DIFF:
     434             :                 case ACTION_DIFF_LONG:
     435           0 :                         logp("No second backup specified. Using file system scan.\n");
     436           0 :                         backup2="n"; // For 'next'.
     437             :                 default:
     438             :                         break;
     439             :         }
     440             : 
     441             :         // The logfile option is only used for the status client stuff.
     442           0 :         if(logfile
     443           0 :           && (act!=ACTION_STATUS
     444           0 :                 && act!=ACTION_STATUS_SNAPSHOT))
     445           0 :                         logp("-l <logfile> option obsoleted\n");
     446             : 
     447           0 :         if(orig_client
     448           0 :           && *orig_client
     449           0 :           && set_string(confs[OPT_ORIG_CLIENT], orig_client))
     450             :                 goto end;
     451             : 
     452             :         // The random delay needs to happen before the lock is got, otherwise
     453             :         // you would never be able to use burp by hand.
     454           0 :         if(randomise) set_int(confs[OPT_RANDOMISE], randomise);
     455           0 :         mode=get_e_burp_mode(confs[OPT_BURP_MODE]);
     456           0 :         if(mode==BURP_MODE_CLIENT
     457           0 :           && (act==ACTION_BACKUP_TIMED || act==ACTION_TIMER_CHECK))
     458           0 :                 random_delay(confs);
     459             : 
     460           0 :         if(mode==BURP_MODE_SERVER)
     461             :         {
     462           0 :                 switch(act)
     463             :                 {
     464             :                         case ACTION_CHAMP_CHOOSER:
     465             :                                 // Need to run without getting the lock.
     466             :                                 break;
     467             :                         default:
     468           0 :                                 if(!(lock=get_prog_lock(confs)))
     469             :                                         goto end;
     470             :                                 break;
     471             :                 }
     472             :         }
     473           0 :         else if(mode==BURP_MODE_CLIENT)
     474             :         {
     475           0 :                 switch(act)
     476             :                 {
     477             :                         case ACTION_BACKUP:
     478             :                         case ACTION_BACKUP_TIMED:
     479             :                         case ACTION_TIMER_CHECK:
     480             :                                 // Need to get the lock.
     481           0 :                                 if(!(lock=get_prog_lock(confs)))
     482             :                                         goto end;
     483             :                                 break;
     484             :                         default:
     485             :                                 break;
     486             :                 }
     487             :         }
     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.10