def _waitForDeleteResult(self, request, timeout): """ Executed in a dedicated thread. """ res = False deleteResult = False try: # Let's pool the result regularly, while we have a pending request. resultType, resultData = self._server.result(request, timeout=timeout) self.getLogger().debug('Delete result: %s' % resultData) res = True # resultData is an empty list if OK # deleteResult = resultData deleteResult = True except ldap.USER_CANCELLED: pass except ldap.NO_SUCH_OBJECT: res = True deleteResult = False except Exception as e: self._onError(request, ProbeImplementationManager.getBacktrace()) if res: self._onResult(request, deleteResult, 'delete')
def onTriSend(self, message, sutAddress): try: # FIXME: # Should go to a configured codec instance instead. # (since we modify the message here...) if not message.has_key('version'): message['version'] = self['version'] try: (encodedMessage, summary) = CodecManager.encode('http.request', message) except Exception as e: raise ProbeImplementationManager.ProbeException( 'Invalid request message format: cannot encode HTTP request:\n%s' % ProbeImplementationManager.getBacktrace()) # Connect if needed if not self.isConnected(): self.connect() # Send our payload self._httpConnection.send(encodedMessage) self.logSentPayload(summary, encodedMessage, "%s:%s" % self._httpConnection.getpeername()) # Now wait for a response asynchronously self.waitResponse() except Exception as e: raise ProbeImplementationManager.ProbeException( 'Unable to send HTTP request: %s' % str(e))
def _waitForSearchResult(self, request, timeout): """ Executed in a dedicated thread. """ res = False resultSet = [] try: # Let's pool the result regularly, while we have a pending request. resultType, resultData = self._server.result(request, timeout=timeout) self.getLogger().debug('Search result: %s' % resultData) for (dn, attributes) in resultData: resultSet.append({ 'dn': self._stripBaseDn(dn), 'attributes': attributes }) res = True except ldap.USER_CANCELLED: pass except ldap.NO_SUCH_OBJECT: res = True resultSet = [] except Exception as e: self._onError(request, ProbeImplementationManager.getBacktrace()) if res: self._onResult(request, resultSet, 'search')
def write(self, dn, attributes): """ Synchronous implementation for now... """ dn = self._addBaseDn(dn) if not self._ensureBind(): return completed = False try: try: # a list of tuples (dn, attribute dict) searchResult = self._server.search_s(dn, ldap.SCOPE_BASE) if not searchResult: # Let's add it self._server.add_s(dn, ldap.modlist.addModlist(attributes)) completed = True elif len(searchResult) == 1: # Let's modify the entry # modifyMolist(previous values, new values) self._server.modify_s(dn, ldap.modlist.modifyModlist(searchResult[0][1], attributes, ignore_oldexistent = 1)) completed = True else: # Cannot write multiple DNs. self.triEnqueueMsg(('error', 'Multiple entries correspond to this dn, cannot update'), self['server_url']) except ldap.NO_SUCH_OBJECT: # Let's add it self._server.add_s(dn, ldap.modlist.addModlist(attributes)) completed = True except Exception, e: self.triEnqueueMsg(('error', 'Error while updating/adding some data:\n%s' % ProbeImplementationManager.getBacktrace()), self['server_url'])
def run(self): # Main poll loop while not self._stopEvent.isSet(): try: listening = self._probe._getListeningSockets() # active is a dict of socket: peerAddress active = self._probe._getActiveSockets() rset = listening + active.keys() r, w, e = select.select(rset, [], [], 0.001) for s in r: try: if s in listening: self._probe.getLogger().debug( "Accepting a new connection") (sock, addr) = s.accept() if self._probe['use_ssl']: sock = self._probe._toSsl(sock, serverSide=True) self._probe._onIncomingConnection(sock, addr) # Raise a new connection notification event - soon else: # Active socket. We get its peername from its registration, not via s.getpeername() # as the remote endpoint might have sent a RST and disconnected, while we still have some data to read for it. # Calling s.getpeername() would then fail, preventing us from reading that remaining data. addr = active.get(s) self._probe.getLogger().debug( "New data to read from %s" % str(addr)) data = s.recv(65535) if not data: self._probe.getLogger().debug( "%s disconnected by peer" % str(addr)) self._probe._feedData( addr, '' ) # notify the feeder that we won't have more data self._probe._disconnect( addr, reason="disconnected by peer") else: # New received message. self._probe._feedData(addr, data) except Exception as e: self._probe.getLogger().warning( "exception while polling active/listening sockets: %s" % str(e) + ProbeImplementationManager.getBacktrace()) except Exception as e: self._probe.getLogger().warning( "exception while polling active/listening sockets: %s" % str(e)) # Avoid 100% CPU usage when select() raises an error time.sleep(0.01)
def _send(self, conn, data): encoder = self['default_encoder'] if encoder: try: (data, summary) = CodecManager.encode(encoder, data) except Exception: raise ProbeImplementationManager.ProbeException( 'Cannot encode outgoing message using defaut encoder:\n%s' % ProbeImplementationManager.getBacktrace()) self.logSentPayload(summary, data, "%s:%s" % conn.socket.getpeername()) else: self.logSentPayload("TCP data", data, "%s:%s" % conn.socket.getpeername()) conn.socket.send(data)
def write(self, dn, attributes): """ Synchronous implementation for now... """ dn = self._addBaseDn(dn) if not self._ensureBind(): return completed = False try: try: # a list of tuples (dn, attribute dict) searchResult = self._server.search_s(dn, ldap.SCOPE_BASE) if not searchResult: # Let's add it self._server.add_s(dn, ldap.modlist.addModlist(attributes)) completed = True elif len(searchResult) == 1: # Let's modify the entry # modifyMolist(previous values, new values) self._server.modify_s( dn, ldap.modlist.modifyModlist(searchResult[0][1], attributes, ignore_oldexistent=1)) completed = True else: # Cannot write multiple DNs. self.triEnqueueMsg(( 'error', 'Multiple entries correspond to this dn, cannot update' ), self['server_url']) except ldap.NO_SUCH_OBJECT: # Let's add it self._server.add_s(dn, ldap.modlist.addModlist(attributes)) completed = True except Exception as e: self.triEnqueueMsg( ('error', 'Error while updating/adding some data:\n%s' % ProbeImplementationManager.getBacktrace()), self['server_url']) if completed: self.triEnqueueMsg(('writeResult', True))
def _waitForSearchResult(self, request, timeout): """ Executed in a dedicated thread. """ res = False resultSet = [] try: # Let's pool the result regularly, while we have a pending request. resultType, resultData = self._server.result(request, timeout = timeout) self.getLogger().debug('Search result: %s' % resultData) for (dn, attributes) in resultData: resultSet.append({'dn': self._stripBaseDn(dn), 'attributes': attributes}) res = True except ldap.USER_CANCELLED: pass except ldap.NO_SUCH_OBJECT: res = True resultSet = [] except Exception, e: self._onError(request, ProbeImplementationManager.getBacktrace())
def run(self): self._probe.getLogger().debug("Starting command execution thread...") try: if isinstance(self._command, list): self._process = pexpect.spawn(self._command[0], self._command[1:]) else: # Assume this is a string self._process = pexpect.spawn(self._command) self._process.setwinsize(24, 80) except Exception as e: self._probe.triEnqueueMsg( 'Internal execution error: %s' % ProbeImplementationManager.getBacktrace()) retcode = None alive = True while alive: alive = self._process.isalive() try: r = self._process.read_nonblocking(1024, self._timeout) self.handleOutput(r, 'stdout') except: time.sleep(0.001) # Consume the whole output try: while 1: r = self._process.read_nonblocking(1024, self._timeout) self.handleOutput(r, 'stdout') except: pass # nothing more to read self._process.close() retcode = self._process.status self._process = None self._probe._onExecThreadTerminated() if self.getReportStatus(): self._probe.triEnqueueMsg({'status': retcode}) self._stoppedEvent.set()
def _waitForDeleteResult(self, request, timeout): """ Executed in a dedicated thread. """ res = False deleteResult = False try: # Let's pool the result regularly, while we have a pending request. resultType, resultData = self._server.result(request, timeout = timeout) self.getLogger().debug('Delete result: %s' % resultData) res = True # resultData is an empty list if OK # deleteResult = resultData deleteResult = True except ldap.USER_CANCELLED: pass except ldap.NO_SUCH_OBJECT: res = True deleteResult = False except Exception, e: self._onError(request, ProbeImplementationManager.getBacktrace())