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