Esempio n. 1
0
 def ntp_setup(self):
     try:
         self.time_manager = TimeManager()
         self.root_dispersion = self.time_manager.get_time_error()
         log.info(
             "We believe this system's clock is accurate to within %f seconds"
             % self.root_dispersion)
         if self.root_dispersion > 5.0:
             log.error(
                 "Sorry, your clock is out by more than 5 secs. Switzerland isn't going to work!"
             )
             sys.exit(1)
     except UncertainTimeError:
         if not self.config.allow_uncertain_time:
             print "  Please fix NTP!!\n"
             print "  (If that is not possible, you can specify an error bound on your system clock"
             print "  using the -u flag, but you must ensure that it is correct. Failure to do so"
             print "  may result in false reports of packet modification.)"
             print "  see http://switzerland.wiki.sourceforge.net/NTP for further details."
             sys.exit(1)
         else:
             #log.warn("NTP is not working:\n"+traceback.format_exc())
             log.warn(
                 "but allow_uncertain_time is set so we're defaulting the clock error to %f"
                 % self.config.manual_clock_error)
             self.root_dispersion = self.config.manual_clock_error
             self.time_manager.method = "blind faith"
     self.params["clock dispersion"] = self.root_dispersion
Esempio n. 2
0
 def ntp_setup(self):
     try:
         self.time_manager = TimeManager()
         self.root_dispersion = self.time_manager.get_time_error()
         log.info("We believe this system's clock is accurate to within %f seconds" % self.root_dispersion)
         if self.root_dispersion > 5.0:
           log.error("Sorry, your clock is out by more than 5 secs. Switzerland isn't going to work!")
           sys.exit(1)
     except UncertainTimeError:
         if not self.config.allow_uncertain_time:
             print "  Please fix NTP!!\n"
             print "  (If that is not possible, you can specify an error bound on your system clock"
             print "  using the -u flag, but you must ensure that it is correct. Failure to do so"
             print "  may result in false reports of packet modification.)"
             print "  see http://switzerland.wiki.sourceforge.net/NTP for further details."
             sys.exit(1)
         else:
             #log.warn("NTP is not working:\n"+traceback.format_exc())
             log.warn("but allow_uncertain_time is set so we're defaulting the clock error to %f" % self.config.manual_clock_error)
             self.root_dispersion = self.config.manual_clock_error
             self.time_manager.method = "blind faith"
     self.params["clock dispersion"] = self.root_dispersion
Esempio n. 3
0
class Alice:
    """
    Alice is the Switzerland client.
    config is an AliceConfig object saying how she should operate;
    linkobj is only changed in test cases
    """
    def __init__(self, config, linkobj=AliceLink):

        self.config = config

        #logging.basicConfig(level=config.log_level,
        #                    format="[%(name)s]: %(message)s")
        config.check()

        self.init_logging()

        self.params = {}
        if config.use_ntp:
            self.ntp_setup()

        # local address may be fake for testing
        self.fm = FlowManager(config, parent=self)
        self.quit_event = threading.Event()
        self.cleaner = Cleaner.Cleaner(self)

        self.listener = PacketListener.PacketListener(self)
        self.link = linkobj(self.quit_event, self, config)
        self.reporter = Reporter.Reporter(self)
        self.tr = Tracerouter()

        self.cleaner.setDaemon(True)
        self.listener.setDaemon(True)
        self.reporter.setDaemon(True)
        self.link.setDaemon(True)
        self.tr.setDaemon(True)

        # XXX we need a privacy setting to disable this, since it might in some
        # cases create link between separate client sessions at separate IPs.
        # But this is about the minimum amount of information we need to spot
        # weird platform specific crashes in early releases
        version = (platform.system(), platform.release(), \
                    platform.machine(), sys.version_info)
        self.params["version"] = version

        log.debug("Starting AliceLink...")
        self.link.start()
        self.tr.start()  # do something while we wait
        self.link.ready.wait(20.0)
        if self.link.quit_event.isSet():
            # error during handshake
            self.listener.cleanup()
            sys.exit(0)
        if not self.link.ready.isSet():
            log.error("failed to start link with switzerland")
            sys.exit(1)

        if config.use_ntp:
            self.link.send_message("parameters", [self.params])

    def init_logging(self):
        "Setup all logging for the client"
        global log

        rootlogger = logging.getLogger()
        if not self.config.quiet:
            output = logging.StreamHandler(sys.stdout)
            rootlogger.addHandler(output)

        # now remove the temporary stderr output we were using during startup
        log.removeHandler(startup_handler)
        log = rootlogger

        if self.config.logfile not in ("-", None):
            fileout = logging.FileHandler(self.config.logfile)
            rootlogger.addHandler(fileout)
            log.error("Logging events to " + self.config.logfile)

        if self.config.pcap_logdir not in ("-", None):
            self.pcap_logger = PcapLogger(self.config.pcap_logdir)
        else:
            self.pcap_logger = None

    def start(self):
        "Launch various sub-threads."
        #self.listener.start()
        self.reporter.start()
        if self.config.do_cleaning:
            self.cleaner.start()
        try:
            import psyco
            psyco.profile()
        except ImportError:
            log.info(
                "The psyco package is unavailable (that's okay, but the client is more\nefficient if psyco is installed)."
            )

    def ntp_setup(self):
        try:
            self.time_manager = TimeManager()
            self.root_dispersion = self.time_manager.get_time_error()
            log.info(
                "We believe this system's clock is accurate to within %f seconds"
                % self.root_dispersion)
            if self.root_dispersion > 5.0:
                log.error(
                    "Sorry, your clock is out by more than 5 secs. Switzerland isn't going to work!"
                )
                sys.exit(1)
        except UncertainTimeError:
            if not self.config.allow_uncertain_time:
                print "  Please fix NTP!!\n"
                print "  (If that is not possible, you can specify an error bound on your system clock"
                print "  using the -u flag, but you must ensure that it is correct. Failure to do so"
                print "  may result in false reports of packet modification.)"
                print "  see http://switzerland.wiki.sourceforge.net/NTP for further details."
                sys.exit(1)
            else:
                #log.warn("NTP is not working:\n"+traceback.format_exc())
                log.warn(
                    "but allow_uncertain_time is set so we're defaulting the clock error to %f"
                    % self.config.manual_clock_error)
                self.root_dispersion = self.config.manual_clock_error
                self.time_manager.method = "blind faith"
        self.params["clock dispersion"] = self.root_dispersion

    def shutdown(self):
        logging.shutdown()
        self.link.send_message("signoff")
Esempio n. 4
0
class Alice:
    """
    Alice is the Switzerland client.
    config is an AliceConfig object saying how she should operate;
    linkobj is only changed in test cases
    """
    def __init__(self, config, linkobj=AliceLink):

        self.config = config

        #logging.basicConfig(level=config.log_level,
        #                    format="[%(name)s]: %(message)s")
        config.check()

        self.init_logging()
         
        self.params = {}
        if config.use_ntp:
            self.ntp_setup()

        # local address may be fake for testing
        self.fm = FlowManager(config, parent=self)
        self.quit_event = threading.Event()
        self.cleaner = Cleaner.Cleaner(self)
        
        self.listener = PacketListener.PacketListener(self)
        self.link = linkobj(self.quit_event, self, config)
        self.reporter = Reporter.Reporter(self)
        self.tr = Tracerouter()
    
        self.cleaner.setDaemon(True)
        self.listener.setDaemon(True)
        self.reporter.setDaemon(True)
        self.link.setDaemon(True)
        self.tr.setDaemon(True)

        # XXX we need a privacy setting to disable this, since it might in some
        # cases create link between separate client sessions at separate IPs.
        # But this is about the minimum amount of information we need to spot
        # weird platform specific crashes in early releases
        version = (platform.system(), platform.release(), \
                    platform.machine(), sys.version_info)
        self.params["version"] = version
        
        log.debug("Starting AliceLink...")
        self.link.start()
        self.tr.start() # do something while we wait
        self.link.ready.wait(20.0)
        if self.link.quit_event.isSet():
          # error during handshake
          self.listener.cleanup()
          sys.exit(0)
        if not self.link.ready.isSet():
            log.error("failed to start link with switzerland")
            sys.exit(1)
        
        if config.use_ntp:
            self.link.send_message("parameters", [self.params])

    def init_logging(self):
        "Setup all logging for the client"
        global log

        rootlogger = logging.getLogger()
        if not self.config.quiet:
            output = logging.StreamHandler(sys.stdout)
            rootlogger.addHandler(output)

        # now remove the temporary stderr output we were using during startup
        log.removeHandler(startup_handler)
        log = rootlogger

        if self.config.logfile not in ("-", None)  :
            fileout = logging.FileHandler(self.config.logfile)
            rootlogger.addHandler(fileout)
            log.error("Logging events to " + self.config.logfile)

        if self.config.pcap_logdir not in ("-", None):
            self.pcap_logger = PcapLogger(self.config.pcap_logdir)
        else:
            self.pcap_logger = None


    def start(self):
        "Launch various sub-threads."
        #self.listener.start()
        self.reporter.start()
        if self.config.do_cleaning:
            self.cleaner.start()
        try:
            import psyco
            psyco.profile()
        except ImportError:
            log.info("The psyco package is unavailable (that's okay, but the client is more\nefficient if psyco is installed).")

    def ntp_setup(self):
        try:
            self.time_manager = TimeManager()
            self.root_dispersion = self.time_manager.get_time_error()
            log.info("We believe this system's clock is accurate to within %f seconds" % self.root_dispersion)
            if self.root_dispersion > 5.0:
              log.error("Sorry, your clock is out by more than 5 secs. Switzerland isn't going to work!")
              sys.exit(1)
        except UncertainTimeError:
            if not self.config.allow_uncertain_time:
                print "  Please fix NTP!!\n"
                print "  (If that is not possible, you can specify an error bound on your system clock"
                print "  using the -u flag, but you must ensure that it is correct. Failure to do so"
                print "  may result in false reports of packet modification.)"
                print "  see http://switzerland.wiki.sourceforge.net/NTP for further details."
                sys.exit(1)
            else:
                #log.warn("NTP is not working:\n"+traceback.format_exc())
                log.warn("but allow_uncertain_time is set so we're defaulting the clock error to %f" % self.config.manual_clock_error)
                self.root_dispersion = self.config.manual_clock_error
                self.time_manager.method = "blind faith"
        self.params["clock dispersion"] = self.root_dispersion

    def shutdown(self):
        logging.shutdown()
        self.link.send_message("signoff")