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-31 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             :         return 0;
     115             : }
     116             : 
     117             : static int replace_conf_str(struct conf *conf, const char *newval)
     118             : {
     119           0 :         if(!newval) return 0;
     120           0 :         return set_string(conf, newval);
     121             : }
     122             : 
     123             : static void usage(void)
     124             : {
     125           0 :         usage_server();
     126           0 :         usage_client();
     127             : }
     128             : 
     129           0 : static int parse_action(enum action *act, const char *optarg)
     130             : {
     131           0 :         if(!strncmp(optarg, "backup", 1))
     132           0 :                 *act=ACTION_BACKUP;
     133           0 :         else if(!strncmp(optarg, "timedbackup", 1))
     134           0 :                 *act=ACTION_BACKUP_TIMED;
     135           0 :         else if(!strncmp(optarg, "Timercheck", 1))
     136           0 :                 *act=ACTION_TIMER_CHECK;
     137           0 :         else if(!strncmp(optarg, "restore", 1))
     138           0 :                 *act=ACTION_RESTORE;
     139           0 :         else if(!strncmp(optarg, "verify", 1))
     140           0 :                 *act=ACTION_VERIFY;
     141           0 :         else if(!strncmp(optarg, "list", 1))
     142           0 :                 *act=ACTION_LIST;
     143           0 :         else if(!strncmp(optarg, "List", 1))
     144           0 :                 *act=ACTION_LIST_LONG;
     145           0 :         else if(!strncmp(optarg, "status", 1))
     146           0 :                 *act=ACTION_STATUS;
     147           0 :         else if(!strncmp(optarg, "Status", 1))
     148           0 :                 *act=ACTION_STATUS_SNAPSHOT;
     149           0 :         else if(!strncmp(optarg, "estimate", 1))
     150           0 :                 *act=ACTION_ESTIMATE;
     151             :         // Make them spell 'delete' out fully so that it is less likely to be
     152             :         // used accidently.
     153           0 :         else if(!strncmp_w(optarg, "delete"))
     154           0 :                 *act=ACTION_DELETE;
     155           0 :         else if(!strncmp(optarg, "champchooser", 1))
     156           0 :                 *act=ACTION_CHAMP_CHOOSER;
     157           0 :         else if(!strncmp(optarg, "diff", 1))
     158           0 :                 *act=ACTION_DIFF;
     159           0 :         else if(!strncmp(optarg, "Diff", 1))
     160           0 :                 *act=ACTION_DIFF_LONG;
     161           0 :         else if(!strncmp(optarg, "monitor", 1))
     162           0 :                 *act=ACTION_MONITOR;
     163             :         else
     164             :         {
     165             :                 usage();
     166           0 :                 return -1;
     167             :         }
     168             :         return 0;
     169             : }
     170             : 
     171             : #ifndef HAVE_WIN32
     172           0 : static int run_champ_chooser(struct conf **confs)
     173             : {
     174           0 :         const char *orig_client=get_string(confs[OPT_ORIG_CLIENT]);
     175           0 :         if(orig_client && *orig_client)
     176           0 :                 return champ_chooser_server_standalone(confs);
     177           0 :         logp("No client name given for standalone champion chooser process.\n");
     178           0 :         logp("Try using the '-C' option.\n");
     179           0 :         return 1;
     180             : }
     181             : 
     182           0 : static int server_modes(enum action act,
     183             :         const char *conffile, struct lock *lock, int generate_ca_only,
     184             :         struct conf **confs)
     185             : {
     186           0 :         switch(act)
     187             :         {
     188             :                 case ACTION_CHAMP_CHOOSER:
     189             :                         // We are running on the server machine, wanting to
     190             :                         // be a standalone champion chooser process.
     191           0 :                         return run_champ_chooser(confs);
     192             :                 default:
     193           0 :                         return server(confs, conffile, lock, generate_ca_only);
     194             :         }
     195             : }
     196             : #endif
     197             : 
     198           0 : static void random_delay(struct conf **confs)
     199             : {
     200             :         int delay;
     201           0 :         int randomise=get_int(confs[OPT_RANDOMISE]);
     202           0 :         if(!randomise) return;
     203           0 :         srand(getpid());
     204           0 :         delay=rand()%randomise;
     205           0 :         logp("Sleeping %d seconds\n", delay);
     206           0 :         sleep(delay);
     207             : }
     208             : 
     209           0 : static int run_test_confs(struct conf **confs, const char *client)
     210             : {
     211           0 :         int ret=-1;
     212           0 :         struct conf **cconfs=NULL;
     213           0 :         if(!client)
     214             :         {
     215           0 :                 confs_dump(confs, 0);
     216           0 :                 ret=0;
     217           0 :                 goto end;
     218             :         }
     219           0 :         if(!(cconfs=confs_alloc()))
     220             :                 goto end;
     221           0 :         confs_init(cconfs);
     222           0 :         if(set_string(cconfs[OPT_CNAME], client)
     223           0 :           || set_string(cconfs[OPT_PEER_VERSION], PACKAGE_VERSION)
     224           0 :           || conf_load_clientconfdir(confs, cconfs))
     225             :                 goto end;
     226           0 :         confs_dump(cconfs, CONF_FLAG_CC_OVERRIDE|CONF_FLAG_INCEXC);
     227             : 
     228             : end:
     229           0 :         confs_free(&cconfs);
     230           0 :         return ret;
     231             : }
     232             : 
     233           0 : static struct lock *get_prog_lock(struct conf **confs)
     234             : {
     235           0 :         struct lock *lock=NULL;
     236           0 :         const char *lockfile=confs_get_lockfile(confs);
     237           0 :         if(!(lock=lock_alloc_and_init(lockfile)))
     238             :                 goto error;
     239           0 :         lock_get(lock);
     240           0 :         switch(lock->status)
     241             :         {
     242             :                 case GET_LOCK_GOT:
     243             :                         return lock;
     244             :                 case GET_LOCK_NOT_GOT:
     245           0 :                         logp("Could not get lockfile.\n");
     246           0 :                         logp("Another process is probably running.\n");
     247           0 :                         goto error;
     248             :                 case GET_LOCK_ERROR:
     249             :                 default:
     250           0 :                         logp("Could not get lockfile.\n");
     251           0 :                         logp("Maybe you do not have permissions to write to %s.\n", lockfile);
     252           0 :                         goto error;
     253             :         }
     254             : error:
     255           0 :         lock_free(&lock);
     256           0 :         return NULL;
     257             : }
     258             : 
     259             : #ifdef HAVE_WIN32
     260             : #define main RealMain
     261             : #endif
     262             : #ifndef UTEST
     263             : static
     264             : #endif
     265           0 : int real_main(int argc, char *argv[])
     266             : {
     267           0 :         int ret=1;
     268           0 :         int option=0;
     269           0 :         int daemon=1;
     270           0 :         int forking=1;
     271           0 :         int strip=0;
     272           0 :         int randomise=0;
     273           0 :         struct lock *lock=NULL;
     274           0 :         struct conf **confs=NULL;
     275           0 :         int forceoverwrite=0;
     276           0 :         enum action act=ACTION_LIST;
     277           0 :         const char *backup=NULL;
     278           0 :         const char *backup2=NULL;
     279           0 :         char *restoreprefix=NULL;
     280           0 :         char *stripfrompath=NULL;
     281           0 :         const char *regex=NULL;
     282           0 :         const char *browsefile=NULL;
     283           0 :         char *browsedir=NULL;
     284           0 :         const char *conffile=config_default_path();
     285           0 :         const char *orig_client=NULL;
     286           0 :         const char *logfile=NULL;
     287             :         // The orig_client is the original client that the normal client
     288             :         // would like to restore from.
     289             : #ifndef HAVE_WIN32
     290           0 :         int generate_ca_only=0;
     291             : #endif
     292           0 :         int vss_restore=1;
     293           0 :         int test_confs=0;
     294             :         enum burp_mode mode;
     295             : 
     296           0 :         log_init(argv[0]);
     297             : #ifndef HAVE_WIN32
     298           0 :         if(!strcmp(prog, "bedup"))
     299           0 :                 return run_bedup(argc, argv);
     300           0 :         if(!strcmp(prog, "bsigs"))
     301           0 :                 return run_bsigs(argc, argv);
     302           0 :         if(!strcmp(prog, "bsparse"))
     303           0 :                 return run_bsparse(argc, argv);
     304             : #endif
     305             : 
     306           0 :         while((option=getopt(argc, argv, "a:b:c:C:d:fFghil:nq:Qr:s:tvxjz:?"))!=-1)
     307             :         {
     308           0 :                 switch(option)
     309             :                 {
     310             :                         case 'a':
     311           0 :                                 if(parse_action(&act, optarg)) goto end;
     312             :                                 break;
     313             :                         case 'b':
     314             :                                 // The diff command may have two backups
     315             :                                 // specified.
     316           0 :                                 if(!backup2 && backup) backup2=optarg;
     317           0 :                                 if(!backup) backup=optarg;
     318             :                                 break;
     319             :                         case 'c':
     320           0 :                                 conffile=optarg;
     321           0 :                                 break;
     322             :                         case 'C':
     323           0 :                                 orig_client=optarg;
     324           0 :                                 break;
     325             :                         case 'd':
     326           0 :                                 restoreprefix=optarg; // for restores
     327           0 :                                 browsedir=optarg; // for lists
     328           0 :                                 break;
     329             :                         case 'f':
     330             :                                 forceoverwrite=1;
     331             :                                 break;
     332             :                         case 'F':
     333           0 :                                 daemon=0;
     334           0 :                                 break;
     335             :                         case 'g':
     336             : #ifndef HAVE_WIN32
     337           0 :                                 generate_ca_only=1;
     338             : #endif
     339           0 :                                 break;
     340             :                         case 'i':
     341           0 :                                 cmd_print_all();
     342           0 :                                 ret=0;
     343           0 :                                 goto end;
     344             :                         case 'l':
     345           0 :                                 logfile=optarg;
     346           0 :                                 break;
     347             :                         case 'n':
     348           0 :                                 forking=0;
     349           0 :                                 break;
     350             :                         case 'q':
     351           0 :                                 randomise=atoi(optarg);
     352           0 :                                 break;
     353             :                         case 'Q':
     354           0 :                                 log_force_quiet();
     355           0 :                                 break;
     356             :                         case 'r':
     357           0 :                                 regex=optarg;
     358           0 :                                 break;
     359             :                         case 's':
     360           0 :                                 strip=atoi(optarg);
     361           0 :                                 break;
     362             :                         case 'v':
     363           0 :                                 printf("%s-%s\n", progname(), PACKAGE_VERSION);
     364           0 :                                 ret=0;
     365           0 :                                 goto end;
     366             :                         case 'x':
     367           0 :                                 vss_restore=0;
     368           0 :                                 break;
     369             :                         case 't':
     370           0 :                                 test_confs=1;
     371           0 :                                 break;
     372             :                         case 'z':
     373           0 :                                 browsefile=optarg;
     374           0 :                                 break;
     375             :                         case 'h':
     376             :                         case '?':
     377             :                         default:
     378             :                                 usage();
     379             :                                 goto end;
     380             :                 }
     381             :         }
     382           0 :         if(optind<argc)
     383             :         {
     384             :                 usage();
     385             :                 goto end;
     386             :         }
     387             : 
     388           0 :         if(act==ACTION_MONITOR)
     389             :         {
     390             :                 // Try to output everything in JSON.
     391           0 :                 log_set_json(1);
     392             : #ifndef HAVE_WIN32
     393             :                 // Need to do this so that processes reading stdout get the
     394             :                 // result of the printfs of logp straight away.
     395           0 :                 setlinebuf(stdout);
     396             : #endif
     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.10