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

Generated by: LCOV version 1.10