LCOV - code coverage report
Current view: top level - src - prog.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 0 224 0.0 %
Date: 2016-01-31 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             :         int oldmax_children, int oldmax_status_children)
     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             :                 setup_signals(oldmax_children, get_int(confs[OPT_MAX_CHILDREN]),
     132             :                         oldmax_status_children,
     133           0 :                         get_int(confs[OPT_MAX_STATUS_CHILDREN]));
     134             : #endif
     135             : 
     136             :         // Do not try to change user or group after the first time.
     137           0 :         if(firsttime && chuser_and_or_chgrp(
     138           0 :                 get_string(confs[OPT_USER]), get_string(confs[OPT_GROUP])))
     139             :                         return -1;
     140             : 
     141           0 :         return 0;
     142             : }
     143             : 
     144             : static int replace_conf_str(struct conf *conf, const char *newval)
     145             : {
     146           0 :         if(!newval) return 0;
     147           0 :         return set_string(conf, newval);
     148             : }
     149             : 
     150             : static void usage(void)
     151             : {
     152           0 :         usage_server();
     153           0 :         usage_client();
     154             : }
     155             : 
     156           0 : static int parse_action(enum action *act, const char *optarg)
     157             : {
     158           0 :         if(!strncmp(optarg, "backup", 1))
     159           0 :                 *act=ACTION_BACKUP;
     160           0 :         else if(!strncmp(optarg, "timedbackup", 1))
     161           0 :                 *act=ACTION_BACKUP_TIMED;
     162           0 :         else if(!strncmp(optarg, "Timercheck", 1))
     163           0 :                 *act=ACTION_TIMER_CHECK;
     164           0 :         else if(!strncmp(optarg, "restore", 1))
     165           0 :                 *act=ACTION_RESTORE;
     166           0 :         else if(!strncmp(optarg, "verify", 1))
     167           0 :                 *act=ACTION_VERIFY;
     168           0 :         else if(!strncmp(optarg, "list", 1))
     169           0 :                 *act=ACTION_LIST;
     170           0 :         else if(!strncmp(optarg, "List", 1))
     171           0 :                 *act=ACTION_LIST_LONG;
     172           0 :         else if(!strncmp(optarg, "status", 1))
     173           0 :                 *act=ACTION_STATUS;
     174           0 :         else if(!strncmp(optarg, "Status", 1))
     175           0 :                 *act=ACTION_STATUS_SNAPSHOT;
     176           0 :         else if(!strncmp(optarg, "estimate", 1))
     177           0 :                 *act=ACTION_ESTIMATE;
     178             :         // Make them spell 'delete' out fully so that it is less likely to be
     179             :         // used accidently.
     180           0 :         else if(!strncmp_w(optarg, "delete"))
     181           0 :                 *act=ACTION_DELETE;
     182           0 :         else if(!strncmp(optarg, "champchooser", 1))
     183           0 :                 *act=ACTION_CHAMP_CHOOSER;
     184           0 :         else if(!strncmp(optarg, "diff", 1))
     185           0 :                 *act=ACTION_DIFF;
     186           0 :         else if(!strncmp(optarg, "Diff", 1))
     187           0 :                 *act=ACTION_DIFF_LONG;
     188           0 :         else if(!strncmp(optarg, "monitor", 1))
     189           0 :                 *act=ACTION_MONITOR;
     190             :         else
     191             :         {
     192             :                 usage();
     193           0 :                 return -1;
     194             :         }
     195             :         return 0;
     196             : }
     197             : 
     198             : #ifndef HAVE_WIN32
     199           0 : static int run_champ_chooser(struct conf **confs)
     200             : {
     201           0 :         const char *orig_client=get_string(confs[OPT_ORIG_CLIENT]);
     202           0 :         if(orig_client && *orig_client)
     203           0 :                 return champ_chooser_server_standalone(confs);
     204           0 :         logp("No client name given for standalone champion chooser process.\n");
     205           0 :         logp("Try using the '-C' option.\n");
     206           0 :         return 1;
     207             : }
     208             : 
     209           0 : static int server_modes(enum action act,
     210             :         const char *conffile, struct lock *lock, int generate_ca_only,
     211             :         struct conf **confs)
     212             : {
     213           0 :         switch(act)
     214             :         {
     215             :                 case ACTION_CHAMP_CHOOSER:
     216             :                         // We are running on the server machine, wanting to
     217             :                         // be a standalone champion chooser process.
     218           0 :                         return run_champ_chooser(confs);
     219             :                 default:
     220           0 :                         return server(confs, conffile, lock, generate_ca_only);
     221             :         }
     222             : }
     223             : #endif
     224             : 
     225           0 : static void random_delay(struct conf **confs)
     226             : {
     227             :         int delay;
     228           0 :         int randomise=get_int(confs[OPT_RANDOMISE]);
     229           0 :         if(!randomise) return;
     230           0 :         srand(getpid());
     231           0 :         delay=rand()%randomise;
     232           0 :         logp("Sleeping %d seconds\n", delay);
     233           0 :         sleep(delay);
     234             : }
     235             : 
     236           0 : static int run_test_confs(struct conf **confs,
     237             :         const char *client, const char *conffile)
     238             : {
     239           0 :         int ret=-1;
     240           0 :         struct conf **cconfs=NULL;
     241           0 :         if(!client)
     242             :         {
     243           0 :                 confs_dump(confs, 0);
     244           0 :                 ret=0;
     245             :                 goto end;
     246             :         }
     247           0 :         if(!(cconfs=confs_alloc()))
     248             :                 goto end;
     249           0 :         confs_init(cconfs);
     250           0 :         if(set_string(cconfs[OPT_CNAME], client)
     251           0 :           || set_string(cconfs[OPT_PEER_VERSION], VERSION)
     252           0 :           || conf_load_clientconfdir(confs, cconfs))
     253             :                 goto end;
     254           0 :         confs_dump(cconfs, CONF_FLAG_CC_OVERRIDE|CONF_FLAG_INCEXC);
     255             : 
     256             : end:
     257           0 :         confs_free(&cconfs);
     258           0 :         return ret;
     259             : }
     260             : 
     261             : #if defined(HAVE_WIN32)
     262             : #define main BurpMain
     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 :         const char *regex=NULL;
     280           0 :         const char *browsefile=NULL;
     281           0 :         char *browsedir=NULL;
     282           0 :         const char *conffile=get_conf_path();
     283           0 :         const char *orig_client=NULL;
     284           0 :         const char *logfile=NULL;
     285             :         // The orig_client is the original client that the normal client
     286             :         // would like to restore from.
     287             : #ifndef HAVE_WIN32
     288           0 :         int generate_ca_only=0;
     289             : #endif
     290           0 :         int vss_restore=1;
     291           0 :         int test_confs=0;
     292             :         enum burp_mode mode;
     293             : 
     294           0 :         log_init(argv[0]);
     295             : #ifndef HAVE_WIN32
     296           0 :         if(!strcmp(prog, "bedup"))
     297           0 :                 return run_bedup(argc, argv);
     298           0 :         if(!strcmp(prog, "bsigs"))
     299           0 :                 return run_bsigs(argc, argv);
     300             : #endif
     301             : 
     302           0 :         while((option=getopt(argc, argv, "a:b:c:C:d:fFghil:nq:r:s:tvxjz:?"))!=-1)
     303             :         {
     304           0 :                 switch(option)
     305             :                 {
     306             :                         case 'a':
     307           0 :                                 if(parse_action(&act, optarg)) goto end;
     308             :                                 break;
     309             :                         case 'b':
     310             :                                 // The diff command may have two backups
     311             :                                 // specified.
     312           0 :                                 if(!backup2 && backup) backup2=optarg;
     313           0 :                                 if(!backup) backup=optarg;
     314             :                                 break;
     315             :                         case 'c':
     316           0 :                                 conffile=optarg;
     317           0 :                                 break;
     318             :                         case 'C':
     319           0 :                                 orig_client=optarg;
     320           0 :                                 break;
     321             :                         case 'd':
     322           0 :                                 restoreprefix=optarg; // for restores
     323           0 :                                 browsedir=optarg; // for lists
     324           0 :                                 break;
     325             :                         case 'f':
     326             :                                 forceoverwrite=1;
     327             :                                 break;
     328             :                         case 'F':
     329           0 :                                 daemon=0;
     330           0 :                                 break;
     331             :                         case 'g':
     332             : #ifndef HAVE_WIN32
     333           0 :                                 generate_ca_only=1;
     334             : #endif
     335           0 :                                 break;
     336             :                         case 'i':
     337           0 :                                 cmd_print_all();
     338           0 :                                 ret=0;
     339           0 :                                 goto end;
     340             :                         case 'l':
     341           0 :                                 logfile=optarg;
     342           0 :                                 break;
     343             :                         case 'n':
     344           0 :                                 forking=0;
     345           0 :                                 break;
     346             :                         case 'q':
     347           0 :                                 randomise=atoi(optarg);
     348           0 :                                 break;
     349             :                         case 'r':
     350           0 :                                 regex=optarg;
     351           0 :                                 break;
     352             :                         case 's':
     353           0 :                                 strip=atoi(optarg);
     354           0 :                                 break;
     355             :                         case 'v':
     356           0 :                                 printf("%s-%s\n", progname(), VERSION);
     357           0 :                                 ret=0;
     358           0 :                                 goto end;
     359             :                         case 'x':
     360           0 :                                 vss_restore=0;
     361           0 :                                 break;
     362             :                         case 't':
     363           0 :                                 test_confs=1;
     364           0 :                                 break;
     365             :                         case 'z':
     366           0 :                                 browsefile=optarg;
     367           0 :                                 break;
     368             :                         case 'h':
     369             :                         case '?':
     370             :                         default:
     371             :                                 usage();
     372             :                                 goto end;
     373             :                 }
     374             :         }
     375           0 :         if(optind<argc)
     376             :         {
     377             :                 usage();
     378             :                 goto end;
     379             :         }
     380             : 
     381           0 :         if(act==ACTION_MONITOR)
     382             :         {
     383             :                 // Try to output everything in JSON.
     384           0 :                 log_set_json(1);
     385             : #ifndef HAVE_WIN32
     386             :                 // Need to do this so that processes reading stdout get the
     387             :                 // result of the printfs of logp straight away.
     388           0 :                 setlinebuf(stdout);
     389             : #endif
     390             :         }
     391             : 
     392           0 :         if(!(confs=confs_alloc()))
     393             :                 goto end;
     394             : 
     395           0 :         if(reload(confs, conffile,
     396             :           1 /* first time */,
     397             :           0 /* no oldmax_children setting */,
     398           0 :           0 /* no oldmax_status_children setting */))
     399             :                 goto end;
     400             : 
     401             :         // Dry run to test config file syntax.
     402           0 :         if(test_confs)
     403             :         {
     404           0 :                 ret=run_test_confs(confs, orig_client, conffile);
     405           0 :                 goto end;
     406             :         }
     407             : 
     408           0 :         if(!backup) switch(act)
     409             :         {
     410             :                 case ACTION_DELETE:
     411           0 :                         logp("No backup specified for deletion.\n");
     412           0 :                         goto end;
     413             :                 case ACTION_RESTORE:
     414             :                 case ACTION_VERIFY:
     415             :                 case ACTION_DIFF:
     416             :                 case ACTION_DIFF_LONG:
     417           0 :                         logp("No backup specified. Using the most recent.\n");
     418           0 :                         backup="0";
     419             :                 default:
     420             :                         break;
     421             :         }
     422           0 :         if(!backup2) switch(act)
     423             :         {
     424             :                 case ACTION_DIFF:
     425             :                 case ACTION_DIFF_LONG:
     426           0 :                         logp("No second backup specified. Using file system scan.\n");
     427           0 :                         backup2="n"; // For 'next'.
     428             :                 default:
     429             :                         break;
     430             :         }
     431             : 
     432             :         // The logfile option is only used for the status client stuff.
     433           0 :         if(logfile
     434           0 :           && (act!=ACTION_STATUS
     435           0 :                 && act!=ACTION_STATUS_SNAPSHOT))
     436           0 :                         logp("-l <logfile> option obsoleted\n");
     437             : 
     438           0 :         if(orig_client
     439           0 :           && *orig_client
     440           0 :           && set_string(confs[OPT_ORIG_CLIENT], orig_client))
     441             :                 goto end;
     442             : 
     443             :         // The random delay needs to happen before the lock is got, otherwise
     444             :         // you would never be able to use burp by hand.
     445           0 :         if(randomise) set_int(confs[OPT_RANDOMISE], randomise);
     446           0 :         mode=get_e_burp_mode(confs[OPT_BURP_MODE]);
     447           0 :         if(mode==BURP_MODE_CLIENT
     448           0 :           && (act==ACTION_BACKUP_TIMED || act==ACTION_TIMER_CHECK))
     449           0 :                 random_delay(confs);
     450             : 
     451           0 :         if(mode==BURP_MODE_SERVER
     452           0 :           && act==ACTION_CHAMP_CHOOSER)
     453             :         {
     454             :                 // These server modes need to run without getting the lock.
     455             :         }
     456           0 :         else if(mode==BURP_MODE_CLIENT
     457           0 :           && (act==ACTION_LIST
     458             :                 || act==ACTION_LIST_LONG
     459           0 :                 || act==ACTION_DIFF
     460           0 :                 || act==ACTION_DIFF_LONG
     461           0 :                 || act==ACTION_STATUS
     462           0 :                 || act==ACTION_STATUS_SNAPSHOT
     463           0 :                 || act==ACTION_MONITOR))
     464             :         {
     465             :                 // These client modes need to run without getting the lock.
     466             :         }
     467             :         else
     468             :         {
     469           0 :                 const char *lockfile=confs_get_lockfile(confs);
     470           0 :                 if(!(lock=lock_alloc_and_init(lockfile)))
     471             :                         goto end;
     472           0 :                 lock_get(lock);
     473           0 :                 switch(lock->status)
     474             :                 {
     475             :                         case GET_LOCK_GOT: break;
     476             :                         case GET_LOCK_NOT_GOT:
     477           0 :                                 logp("Could not get lockfile.\n");
     478           0 :                                 logp("Another process is probably running,\n");
     479           0 :                                 goto end;
     480             :                         case GET_LOCK_ERROR:
     481             :                         default:
     482           0 :                                 logp("Could not get lockfile.\n");
     483           0 :                                 logp("Maybe you do not have permissions to write to %s.\n", lockfile);
     484           0 :                                 goto end;
     485             :                 }
     486             :         }
     487             : 
     488           0 :         set_int(confs[OPT_OVERWRITE], forceoverwrite);
     489           0 :         set_int(confs[OPT_STRIP], strip);
     490           0 :         set_int(confs[OPT_FORK], forking);
     491           0 :         set_int(confs[OPT_DAEMON], daemon);
     492             : 
     493           0 :         strip_trailing_slashes(&restoreprefix);
     494           0 :         strip_trailing_slashes(&browsedir);
     495           0 :         if(replace_conf_str(confs[OPT_BACKUP], backup)
     496           0 :           || replace_conf_str(confs[OPT_BACKUP2], backup2)
     497           0 :           || replace_conf_str(confs[OPT_RESTOREPREFIX], restoreprefix)
     498           0 :           || replace_conf_str(confs[OPT_REGEX], regex)
     499           0 :           || replace_conf_str(confs[OPT_BROWSEFILE], browsefile)
     500           0 :           || replace_conf_str(confs[OPT_BROWSEDIR], browsedir)
     501           0 :           || replace_conf_str(confs[OPT_MONITOR_LOGFILE], logfile))
     502             :                 goto end;
     503             : 
     504           0 :         base64_init();
     505           0 :         hexmap_init();
     506             : 
     507           0 :         if(mode==BURP_MODE_SERVER)
     508             :         {
     509             : #ifdef HAVE_WIN32
     510             :                 logp("Sorry, server mode is not implemented for Windows.\n");
     511             : #else
     512             :                 ret=server_modes(act,
     513           0 :                         conffile, lock, generate_ca_only, confs);
     514             : #endif
     515             :         }
     516             :         else
     517             :         {
     518           0 :                 ret=client(confs, act, vss_restore);
     519             :         }
     520             : 
     521             : end:
     522           0 :         lock_release(lock);
     523           0 :         lock_free(&lock);
     524           0 :         confs_free(&confs);
     525           0 :         return ret;
     526             : }
     527             : 
     528             : #ifndef UTEST
     529             : int main(int argc, char *argv[])
     530             : {
     531             :         return real_main(argc, argv);
     532             : }
     533             : #endif

Generated by: LCOV version 1.10