Exemplo n.º 1
0
    def serve(self):

        controller = self.controller
        threadpool = self.controller.threadpool
        procpool   = self.controller.procpool

        self.logger.info('policy server running on port %s'%self.port)
        while self.stayalive:
            try:
                self.logger.debug('Waiting for connection...')
                sock, addr = self._socket.accept()
                if not self.stayalive:
                    break
                engine = SessionHandler(sock,controller.config,self.plugins)
                self.logger.debug('Incoming connection from %s'%str(addr))
                if threadpool:
                    #this will block if queue is full
                    self.controller.threadpool.add_task(engine)
                elif procpool:
                    # in multi processing, the other process manages configs and plugins itself, we only pass the minimum required information:
                    # a pickled version of the socket (this is no longer required in python 3.4, but in python 2 the multiprocessing queue can not handle sockets
                    # see https://stackoverflow.com/questions/36370724/python-passing-a-tcp-socket-object-to-a-multiprocessing-queue

                    task = forking_dumps(sock)
                    procpool.add_task(task)


                else:
                    engine.handlesession()
            except Exception as e:
                self.logger.exception(e)
Exemplo n.º 2
0
    def serve(self):

        self.logger.info('policy server running on port %s'%self.port)
        while self.stayalive:
            try:
                self.logger.debug('Waiting for connection...')
                sock, addr = self._socket.accept()
                if not self.stayalive:
                    break
                engine = SessionHandler(sock,self.controller.config,self.plugins)
                self.logger.debug('Incoming connection from %s'%str(addr))
                if self.controller.threadpool:
                    #this will block if queue is full
                    self.controller.threadpool.add_task(engine)
                elif self.controller.procpool:
                    # in multi processing, the other process manages configs and plugins itself, we only pass the minimum required information:
                    # a pickled version of the socket (this is no longer required in python 3.4, but in python 2 the multiprocessing queue can not handle sockets
                    # see https://stackoverflow.com/questions/36370724/python-passing-a-tcp-socket-object-to-a-multiprocessing-queue
                    task = forking_dumps(sock)
                    self.controller.procpool.add_task(task)


                else:
                    engine.handlesession()
            except Exception as e:
                self.logger.exception(e)
Exemplo n.º 3
0
def postomaat_process_worker(queue, config, shared_state,child_to_server_messages):
    logging.basicConfig(level=logging.DEBUG)
    workerstate = WorkerStateWrapper(shared_state,'loading configuration')
    logger = logging.getLogger('postomaat.process')

    # load config and plugins
    controller = postomaat.core.MainController(config)
    controller.load_plugins()

    plugins = controller.plugins

    # forward statistics counters to parent process
    stats = Statskeeper()
    stats.stat_listener_callback.append(lambda event: child_to_server_messages.put(event.as_message()))

    try:
        while True:
            workerstate.workerstate = 'waiting for task'
            logger.debug("Child process state: "+workerstate.workerstate)

            # get task
            # Note: The task is a compressed socket
            task = queue.get()

            logger.debug("Child process state: -> got a new task")
            if task is None: # poison pill
                logger.debug("Child process received poison pill - shut down")
                workerstate.workerstate = 'ended'
                return
            workerstate.workerstate = 'starting scan session'

            # recreate socket
            sock = pickle.loads(task)
            handler = SessionHandler(sock, config, plugins)
            handler.handlesession(workerstate)
    except KeyboardInterrupt:
        workerstate.workerstate = 'ended'
    except:
        trb = traceback.format_exc()
        logger.error("Exception in child process: %s"%trb)
        print(trb)
        workerstate.workerstate = 'crashed'
Exemplo n.º 4
0
 def serve(self):
     #disable to debug... 
     use_multithreading=True
     controller=self.controller
     
     self.logger.info('policy server running on port %s'%self.port)
     while self.stayalive:
         try:
             self.logger.debug('Waiting for connection...')
             nsd = self._socket.accept()
             if not self.stayalive:
                 break
             engine = SessionHandler(nsd[0],controller.config,self.plugins)
             self.logger.debug('Incoming connection from %s'%str(nsd[1]))
             if use_multithreading:
                 #this will block if queue is full
                 self.controller.threadpool.add_task(engine)
             else:
                 engine.handlesession()
         except Exception as e:
             self.logger.error('Exception in serve(): %s'%str(e))
Exemplo n.º 5
0
    def serve(self):
        #disable to debug...
        use_multithreading = True
        controller = self.controller

        self.logger.info('policy server running on port %s' % self.port)
        while self.stayalive:
            try:
                self.logger.debug('Waiting for connection...')
                nsd = self._socket.accept()
                if not self.stayalive:
                    break
                engine = SessionHandler(nsd[0], controller.config,
                                        self.plugins)
                self.logger.debug('Incoming connection from %s' % str(nsd[1]))
                if use_multithreading:
                    #this will block if queue is full
                    self.controller.threadpool.add_task(engine)
                else:
                    engine.handlesession()
            except Exception as e:
                self.logger.error('Exception in serve(): %s' % str(e))
Exemplo n.º 6
0
def postomaat_process_worker(queue, config, shared_state,
                             child_to_server_messages, logQueue):

    signal.signal(signal.SIGHUP, signal.SIG_IGN)
    logging.basicConfig(level=logging.DEBUG)
    workerstate = WorkerStateWrapper(shared_state, 'loading configuration')
    logger = logging.getLogger('postomaat.process')

    # Setup address compliance checker
    # -> Due to default linux forking behavior this should already
    #    have the correct setup but it's better not to rely on this
    try:
        address_check = config.get('main', 'address_compliance_checker')
    except Exception as e:
        # might happen for some tests which do not propagate defaults
        address_check = "Default"
    Addrcheck().set(address_check)

    # load config and plugins
    controller = postomaat.core.MainController(config, logQueue)
    controller.load_plugins()

    plugins = controller.plugins

    # forward statistics counters to parent process
    stats = Statskeeper()
    stats.stat_listener_callback.append(
        lambda event: child_to_server_messages.put(event.as_message()))

    try:
        while True:
            workerstate.workerstate = 'waiting for task'
            logger.debug("%s: Child process waiting for task" %
                         logtools.createPIDinfo())
            task = queue.get()
            if task is None:  # poison pill
                logger.debug(
                    "%s: Child process received poison pill - shut down" %
                    logtools.createPIDinfo())
                try:
                    # it might be possible it does not work to properly set the workerstate
                    # since this is a shared variable -> prevent exceptions
                    workerstate.workerstate = 'ended'
                except Exception as e:
                    pass
                finally:
                    return
            workerstate.workerstate = 'starting scan session'

            # recreate socket
            sock = pickle.loads(task)
            handler = SessionHandler(sock, config, plugins)
            handler.handlesession(workerstate)
    except KeyboardInterrupt:
        workerstate.workerstate = 'ended'
    except:
        trb = traceback.format_exc()
        logger.error("Exception in child process: %s" % trb)
        print(trb)
        workerstate.workerstate = 'crashed'
    finally:
        controller.shutdown()