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

Generated by: LCOV version 1.10