def xmlrpc_get_cracker_info(self, ip): if not utils.is_valid_ip_address(ip): logging.warning("Illegal host ip address {}".format(ip)) raise xmlrpc.Fault(101, "Illegal IP address \"{}\".".format(ip)) #logging.info("Getting info for cracker {}".format(ip_address)) cracker = yield controllers.get_cracker(ip) if cracker is None: raise xmlrpc.Fault(104, "Cracker {} unknown".format(ip)) returnValue([]) #logging.info("found cracker: {}".format(cracker)) reports = yield cracker.reports.get() #logging.info("found reports: {}".format(reports)) cracker_cols=['ip_address','first_time', 'latest_time', 'resiliency', 'total_reports', 'current_reports'] report_cols=['ip_address','first_report_time', 'latest_report_time'] returnValue( [cracker.toHash(cracker_cols), [r.toHash(report_cols) for r in reports]] )
class ManagementService(xmlrpc.XMLRPC): """ An example object to be published. """ def __init__(self, staging_dir, service): """Initialise the management service. :param service: Service Facade instance being exposed by this XMLRPC service """ xmlrpc.XMLRPC.__init__(self, allowNone=True) self.service = service self.transaction_counter = 0 if not os.path.exists(staging_dir): raise ValueError("The provided staging directory doesn't exist") self.transactions = {} self.staging_dir = staging_dir self._marshaller = Marshaller() def xmlrpc_insert(self, obj): """ Insert the passed object into the ingester platform """ try: return self._marshaller.obj_to_dict( self.service.persist(self._marshaller.dict_to_obj(obj))) except IngestPlatformError, e: raise translate_exception(e) except Exception, e: logger.exception("Error inserting") raise xmlrpc.Fault(InternalSystemError.__xmlrpc_error__, str(e))
def xmlrpc_provision(self, app_id, path_to_cert_or_cert, environment, timeout=15): """ Starts an APNSService for the this app_id and keeps it running Arguments: app_id the app_id to provision for APNS path_to_cert_or_cert absolute path to the APNS SSL cert or a string containing the .pem file environment either 'sandbox' or 'production' timeout seconds to timeout connection attempts to the APNS server Returns: None """ if environment not in ('sandbox', 'production'): raise xmlrpc.Fault( 401, 'Invalid environment provided `%s`. Valid ' 'environments are `sandbox` and `production`' % (environment, )) if app_id not in self.app_ids: # log.msg('provisioning ' + app_id + ' environment ' + environment) self.app_ids[app_id] = APNSService(path_to_cert_or_cert, environment, timeout)
def _finish_err(r): # so far, the only error that could really become of this # request is a timeout, since APNS simply terminates connectons # that are made unsuccessfully, which twisted will try endlessly # to reconnect to, we timeout and notifify the client raise xmlrpc.Fault( 500, 'Connection to the APNS server could not be made.')
def stop_sniff(self, args, kwargs): """Stops sniffing on specified interfaces and returns captured data. Notes: Redefine standard method because we need convert packet data string before sending over xml. """ try: self.class_logger.debug("Stop sniffing") data_orig = self.pypacker.stop_sniff(*args, **kwargs) data_str = defaultdict(list) for iface in data_orig: for packet in data_orig[iface]: # Store original packet timestamps, to restore them on remote side _time = packet.time data_str[iface].append((_time, packet.bin())) return pickle.dumps(data_str) except: exc_type, exc_value, exc_traceback = sys.exc_info() traceback_message = traceback.format_exception( exc_type, exc_value, exc_traceback) message = "Error while stop_sniff:\n{0}".format( "".join(traceback_message)) self.class_logger.error(message) raise xmlrpc.Fault(500, message)
def render_POST(self, request): request.content.seek(0, 0) request.setHeader("content-type", "text/xml") try: args, functionPath = xmlrpclib.loads(request.content.read()) if args and isinstance(args[-1], dict): # Passing args and kwargs to _ldtp_callback # fail, so using self, kind of work around ! kwargs = args[-1] args = args[:-1] if delay or self._delaycmdexec: pattern = '(wait|exist|has|get|verify|enabled|' pattern += 'launch|image|system)' p = re.compile(pattern) if not p.search(functionPath): # Sleep for 1 second, else the at-spi-registryd dies, # on the speed we execute try: if self._delaycmdexec: self.wait(float(self._delaycmdexec)) else: self.wait(float(delay)) except ValueError: time.sleep(0.5) else: kwargs = {} except Exception, e: f = xmlrpc.Fault( self.FAILURE, "Can't deserialize input: %s" % (e,)) self._cbRender(f, request)
def xmlrpc_joinClient(self, serverId): # RPC server implementation # for master try: self.proxy.connect(serverId) return 0 except Exception as e: return xmlrpc.Fault(1, str(e))
def xmlrpc_search(self, criteria, offset, limit): try: return self._marshaller.obj_to_dict( self.service.search(self._marshaller.dict_to_obj(criteria), offset, limit)) except Exception, e: logger.exception("Error searching") raise xmlrpc.Fault(InternalSystemError.__xmlrpc_error__, str(e))
def unauthorized(self, request): """Render an XMLRPC error indicating an authentication failure. @type request: HTTPRequest @param request: the request for this xmlrpc call. @return: None """ self._cbRender(xmlrpc.Fault(self.FAILURE, "Unauthorized"), request)
def _ebRender(self, failure): """Override the default errback for rendering such that non-Faults are wrapped usefully rather than being converted into an unhelpful 'error'. """ if isinstance(failure.value, xmlrpc.Fault): return failure.value log.err(failure) return xmlrpc.Fault(str(failure.type), str(failure.value))
def xmlrpc_snowman(self, payload): """ Used to test that we can pass Unicode. """ snowman = u"\u2603" if snowman != payload: return xmlrpc.Fault(13, "Payload not unicode snowman") return snowman
def xmlrpc_get_new_hosts(self, request, timestamp, threshold, hosts_added, resiliency): try: x_real_ip = request.received_headers.get("X-Real-IP") remote_ip = x_real_ip or request.getClientIP() logging.debug("get_new_hosts({},{},{},{}) from {}".format( timestamp, threshold, hosts_added, resiliency, remote_ip)) try: timestamp = long(timestamp) threshold = int(threshold) resiliency = long(resiliency) except: logging.warning( "Illegal arguments to get_new_hosts from client {}".format( remote_ip)) raise xmlrpc.Fault(102, "Illegal parameters.") now = time.time() # refuse timestamps from the future if timestamp > now: logging.warning( "Illegal timestamp to get_new_hosts from client {}".format( remote_ip)) raise xmlrpc.Fault(103, "Illegal timestamp.") for host in hosts_added: if not self.is_valid_ip_address(host): logging.warning("Illegal host ip address {}".format(host)) raise xmlrpc.Fault( 101, "Illegal IP address \"{}\".".format(host)) # TODO: maybe refuse timestamp from far past because it will # cause much work? OTOH, denyhosts will use timestamp=0 for # the first run! # TODO: check if client IP is a known cracker result = {} result['timestamp'] = str(long(time.time())) result['hosts'] = yield controllers.get_qualifying_crackers( threshold, resiliency, timestamp, config.max_reported_crackers, set(hosts_added)) logging.debug("returning: {}".format(result)) except xmlrpc.Fault, e: raise e
def render_POST(self, request): request.content.seek(0, 0) request.setHeader("content-type", "text/xml") try: args, functionPath = xmlrpclib.loads(request.content.read()) if args and isinstance(args[-1], dict): # Passing args and kwargs to _ldtp_callback # fail, so using self, kind of work around ! kwargs = args[-1] args = args[:-1] if delay or self._delaycmdexec: pattern = '(wait|exist|has|get|verify|enabled|' pattern += 'launch|image|system)' p = re.compile(pattern) if not p.search(functionPath): # Sleep for 1 second, else the at-spi-registryd dies, # on the speed we execute try: if self._delaycmdexec: self.wait(float(self._delaycmdexec)) else: self.wait(float(delay)) except ValueError: time.sleep(0.5) else: kwargs = {} except Exception as e: f = xmlrpc.Fault(self.FAILURE, "Can't deserialize input: %s" % (e, )) self._cbRender(f, request) else: try: if hasattr(self, 'lookupProcedure'): # Starting twisted 11.1 function = self.lookupProcedure(functionPath) else: function = self._getFunction(functionPath) except xmlrpc.Fault as f: self._cbRender(f, request) else: if _ldtp_debug: debug_st = '%s(%s)' % \ (functionPath, ', '.join(map(repr, args) + \ ['%s=%s' % (k, repr(v)) \ for k, v in kwargs.items()])) print(debug_st) logger.debug(debug_st) if _ldtp_debug_file: with open(_ldtp_debug_file, "a") as fp: fp.write(debug_st) xmlrpc.defer.maybeDeferred(function, *args, **kwargs).\ addErrback(self._ebRender).\ addCallback(self._cbRender, request) return xmlrpc.server.NOT_DONE_YET
def _getFunction(self, functionPath): try: return XMLRPC._getFunction(self, functionPath) except xmlrpc.NoSuchFunction: if functionPath.startswith("SESSION"): raise xmlrpc.Fault(self.SESSION_EXPIRED, "Session non-existant/expired.") else: raise
def lookupProcedure(self, procedurePath): try: return XMLRPC.lookupProcedure(self, procedurePath) except xmlrpc.NoSuchFunction: if procedurePath.startswith("SESSION"): raise xmlrpc.Fault(self.SESSION_EXPIRED, "Session non-existant/expired.") else: raise
def _ebRender(self, failure): """ Custom exception rendering. Ref: https://netzguerilla.net/iro/dev/_modules/iro/view/xmlrpc.html """ if isinstance(failure.value, Exception): msg = """%s: %s""" % (failure.type.__name__, failure.value.args[0]) return xmlrpc.Fault(400, msg) return super(TriggerXMLRPCServer, self)._ebRender(self, failure)
class Server(xmlrpc.XMLRPC): """ An example object to be published. """ @staticmethod def is_valid_ip_address(ip_address): try: ip = ipaddr.IPAddress(ip_address) except: return False if (ip.is_reserved or ip.is_private or ip.is_loopback or ip.is_unspecified or ip.is_multicast or ip.is_link_local): return False return True @withRequest @inlineCallbacks def xmlrpc_add_hosts(self, request, hosts): try: x_real_ip = request.received_headers.get("X-Real-IP") remote_ip = x_real_ip or request.getClientIP() logging.info("add_hosts({}) from {}".format(hosts, remote_ip)) for cracker_ip in hosts: if not self.is_valid_ip_address(cracker_ip): logging.warning( "Illegal host ip address {} from {}".format( cracker_ip, remote_ip)) raise xmlrpc.Fault( 101, "Illegal IP address \"{}\".".format(cracker_ip)) logging.debug("Adding report for {} from {}".format( cracker_ip, remote_ip)) yield utils.wait_and_lock_host(cracker_ip) try: cracker = yield Cracker.find( where=['ip_address=?', cracker_ip], limit=1) if cracker is None: now = time.time() cracker = Cracker(ip_address=cracker_ip, first_time=now, latest_time=now, resiliency=0, total_reports=0, current_reports=0) yield cracker.save() yield controllers.add_report_to_cracker(cracker, remote_ip) finally: utils.unlock_host(cracker_ip) logging.debug("Done adding report for {} from {}".format( cracker_ip, remote_ip)) except xmlrpc.Fault, e: raise e except Exception, e: log.err(_why="Exception in add_hosts") raise xmlrpc.Fault(104, "Error adding hosts: {}".format(str(e)))
def xmlrpc_wrap(func, *args, **kwargs): """Register all Pypacker TG methods. """ try: return func(*args, **kwargs) except: exc_type, exc_value, exc_traceback = sys.exc_info() traceback_message = traceback.format_exception( exc_type, exc_value, exc_traceback) raise xmlrpc.Fault(500, traceback_message)
def _post_remark_interaction(self, trans, user_id, username, remark): tdb = transdb.TransDbAccess(self.db, trans) tree = etree.fromstring(remark.data) remark_node = tree.xpath("/remark")[0] remark_node.attrib['author'] = username prefix = remark_node.attrib['prefix'] prefix_id = tdb.access_to_prefix(prefix, user_id) if prefix_id is None: raise xmlrpc.Fault(-1, "User is not allowed to send remarks for this base") tdb.insert_remark(etree.tostring(tree), prefix_id)
def xmlrpc_pickle_system(self, system_id): """ Pickle the pycpa system on the server-side :param system_id: ID of the system to analyze :type system_id: string :returns: 0 on sucess """ system = self._obj_from_id(system_id, model.System) try: with open('data.pkl', 'wb') as f: pickle.dump(system, f) except RuntimeError: raise xmlrpc.Fault(GENERAL_ERROR, "Error during pickle") return 0
def _login_interaction(self, trans, user, password): tdb = transdb.TransDbAccess(self.service.db, trans) ok, user_id, username = tdb.login(user, password) if ok: token = self.service.sessions.new(user_id, username) return token else: # cfglogin ok, user_id, username = tdb.autologin(user, password) if ok: token = self.service.sessions.new(user_id, username) return token else: raise xmlrpc.Fault(-1, "Incorrect username and password")
def xmlrpc_get_task_result(self, results_id, task_id): """ Obtain the analysis results for a task. :param results_id: ID of the results object :type task_id: string :param task_id: ID of the task :type task_id: string :returns: a dictionary of results for task_id. :rtype: :py:class:`pycpa.analysis.TaskResult` """ results = self._obj_from_id(results_id, dict) task = self._obj_from_id(task_id, model.Task) if task not in results: raise xmlrpc.Fault(INVALID_RESULTS, "no results for task") return results[task]
def xmlrpc_tgcmd(self, method, args=None, kwargs=None): """Store shutdown server command. """ args = args if args is not None else [] kwargs = kwargs if kwargs is not None else {} str_args = ", ".join(map(str, args)) str_kwargs = " ".join( ("{0}={1}".format(k, v) for k, v in kwargs.items())) self.class_logger.info("Command: %s(%s, %s)", method, str_args, str_kwargs) try: rc = getattr(self.pypacker, method)(*args, **kwargs) return rc except Exception as err: self.class_logger.error(str(err)) raise xmlrpc.Fault(500, str(err))
class PeeringServer(xmlrpc.XMLRPC): """ Peering xmlrpc methods """ @withRequest @inlineCallbacks def xmlrpc_update(self, request, key, update): try: logging.info("update({}, {})".format(key, update)) key = key.decode('hex') update = update.decode('base64') yield peering.handle_update(key, update) except xmlrpc.Fault, e: raise e except Exception, e: log.err(_why="Exception in update") raise xmlrpc.Fault(105, "Error in update({},{})".format(key, update))
def xmlrpc_set_id_type(self, id_type): ''' Select the type for returned IDs. 'numeric' generates numeric IDs (strings of long int) 'id_numeric' like 'numeric', but prefixes 'id_' (makes debug output executable) 'name' generates the ID from the objects' name 'full' is like 'name', but prefixes name by parent's name (TODO) In case of 'name' or 'full', the ID is suffixed in case of duplicates. :param id_type: 'numeric', 'id_numeric', 'name', or 'full' :type id_type: string :returns: 0 ''' if id_type in {'numeric', 'id_numeric', 'name', 'full'}: self.id_type = id_type else: raise xmlrpc.Fault(GENERAL_ERROR, 'invalid id type') return 0
def xmlrpc_assign_scheduler(self, resource_id, scheduler_string): """ Assign a scheduler to a resource. See :func:`xmlrpc_get_valid_schedulers` for a list of valid schedulers. :param resource_id: ID of the resource to which to assign the scheduler. :type resource_id: integer :param scheduler_string: Identifies the type of scheduler to set. :type scheduler_string: string :returns: 0 for success """ scheduler_string = str(scheduler_string) resource = self._obj_from_id(resource_id, model.Resource) scheduler = self.scheduling_policies.get(scheduler_string, None) if scheduler is None: logger.error("invalid scheduler %s selected", scheduler_string) raise xmlrpc.Fault(INVALID_SCHEDULER, "invalid scheduler") logger.debug("{}assign_scheduler({}, '{}')".format( self.debug_prefix, resource_id, scheduler_string)) resource.scheduler = scheduler() return 0
def xmlrpc_graph_system(self, system_id, filename): """ Generate a graph of the system (in server directory). It uses graphviz for plotting, so the 'dot' command must be in the PATH of the server environment. :param system_id: ID of the system to analyze :type system_id: string :param filename: File name (relative to server working directory) to which to store the graph. :type filename: string :returns: 0 """ try: import pygraphviz except ImportError: raise xmlrpc.Fault(GENERAL_ERROR, "graph not supported on this platform.") s = self._obj_from_id(system_id, model.System) graph.graph_system(s, filename) return 0
def _cbRender(self, result, request, responseFailed=None): """Override method to do threaded xmlrpclib.dump. This is ugly and non-portable, but will have to do for now. """ if responseFailed: return if isinstance(result, xmlrpc.Handler): result = result.result if not isinstance(result, xmlrpc.Fault): result = (result, ) try: try: content = yield self.threadDumpResult(result) except Exception, e: f = xmlrpc.Fault(self.FAILURE, "Can't serialize output: %s" % (e, )) content = xmlrpclib.dumps(f, methodresponse=True, allow_none=self.allowNone) request.setHeader("content-length", str(len(content))) request.write(content)
def xmlrpc_add_hosts(self, request, hosts): try: x_real_ip = request.received_headers.get("X-Real-IP") remote_ip = x_real_ip or request.getClientIP() logging.info("add_hosts({}) from {}".format(hosts, remote_ip)) for cracker_ip in hosts: if not self.is_valid_ip_address(cracker_ip): logging.warning( "Illegal host ip address {} from {}".format( cracker_ip, remote_ip)) raise xmlrpc.Fault( 101, "Illegal IP address \"{}\".".format(cracker_ip)) logging.debug("Adding report for {} from {}".format( cracker_ip, remote_ip)) yield utils.wait_and_lock_host(cracker_ip) try: cracker = yield Cracker.find( where=['ip_address=?', cracker_ip], limit=1) if cracker is None: now = time.time() cracker = Cracker(ip_address=cracker_ip, first_time=now, latest_time=now, resiliency=0, total_reports=0, current_reports=0) yield cracker.save() yield controllers.add_report_to_cracker(cracker, remote_ip) finally: utils.unlock_host(cracker_ip) logging.debug("Done adding report for {} from {}".format( cracker_ip, remote_ip)) except xmlrpc.Fault, e: raise e
def check(self, token, *args, **kwargs): ok, userid, username = self.service.sessions.check(token) if ok == True: return f(self, userid, username, *args, **kwargs) else: raise xmlrpc.Fault(-1, "Incorrect session token")