def _listIdentifiers(self): s = "%sverb=ListIdentifiers&" % (self.server) s += urllib.urlencode(self.params) resp = self._fetchStream(s) data = resp.read() # self.lastResponse = resp # Now use existing infrastructure to parse doc = StringDocument(data, self.id, mimeType='text/xml') rec = BSParser.process_document(None, doc) dom = rec.get_dom(session) for top in dom.childNodes: if (top.nodeType == elementType): break for c in top.childNodes: if (c.nodeType == elementType and c.localName == 'ListIdentifiers'): for c2 in c.childNodes: if (c2.nodeType == elementType and c2.localName == 'header'): for c3 in c2.childNodes: if (c3.nodeType == elementType and c3.localName == 'identifier'): self.ids.append(getFirstData(c3)) elif (c2.nodeType == elementType and c2.localName == 'resumptionToken'): t = getFirstData(c2) if (t): self.token = t try: self.total = c2.getAttr('completeListSize') except: pass
def _handleConfigNode(self, session, node): if (node.localName in self.simpleNodes): setattr(self, node.localName, getFirstData(node)) elif (node.localName == "flags"): # Extract Rights info # <flags> <flag> <object> <value> </flag> </flags> for c in node.childNodes: if c.nodeType == elementType and c.localName == "flag": obj = None flag = None for c2 in c.childNodes: if c2.nodeType == elementType: if c2.localName == "object": obj = getFirstData(c2) elif c2.localName == "value": flag = getFirstData(c2) if (flag not in self.allFlags) and (flag[:4] != "c3fn"): raise ConfigFileException("Unknown flag: %s" % flag) if obj == None or flag == None: raise ConfigFileException("Missing object or value element for flag for user %s" % self.username) if (obj): f = self.flags.get(flag, []) if f != "": f.append(obj) self.flags[flag] = f else: self.flags[flag] = "" elif (node.localName == "history"): # Extract user history pass elif (node.localName == "hostmask"): # Extract allowed hostmask list pass
def _recurseSubConfigs(self, session, child): for mod in child.childNodes: if mod.nodeType == elementType and mod.localName == "subConfig": id = mod.getAttributeNS(None, 'id') self.subConfigs[id] = mod # Cache indexes and maps type = mod.getAttributeNS(None, 'type') if type == 'index': self.indexConfigs[id] = mod elif type == 'protocolMap': self.protocolMapConfigs[id] = mod elif type == 'database': self.databaseConfigs[id] = mod elif type == '': msg = "Object must have a type attribute: %s" % id raise ConfigFileException(msg) elif mod.nodeType == elementType and mod.localName == "path": if (mod.hasAttributeNS(None, 'type') and mod.getAttributeNS(None, 'type') == 'includeConfigs'): # Import into our space if (mod.hasAttributeNS(None, 'ref')): # <path type="includeConfigs" ref="configStore"/> self._includeConfigStores.append( mod.getAttributeNS(None, 'ref') ) else: # <path type="includeConfigs">path/to/file.xml</path> path = getFirstData(mod) # Expand user-specific paths path = os.path.expanduser(path) if not os.path.isabs(path): path = os.path.join( self.get_path(session, 'defaultPath'), path) if os.path.isdir(path): # include all configs in it at our space files = glob.glob("%s/*.xml" % path) for f in files: self._parseIncludes(session, f) else: self._parseIncludes(session, path) else: path = getFirstData(mod) # Expand user-specific paths path = os.path.expanduser(path) if not os.path.isabs(path): path = os.path.join( self.get_path(session, 'defaultPath'), path) dom = self._getDomFromFile(session, path) id = mod.getAttributeNS(None, 'id') self.subConfigs[id] = dom.childNodes[0] ot = mod.getAttributeNS(None, 'type') if ot == 'database': self.databaseConfigs[id] = dom.childNodes[0]
def _recurseSubConfigs(self, session, child): for mod in child.childNodes: if mod.nodeType == elementType and mod.localName == "subConfig": id = mod.getAttributeNS(None, 'id') self.subConfigs[id] = mod # Cache indexes and maps type = mod.getAttributeNS(None, 'type') if type == 'index': self.indexConfigs[id] = mod elif type == 'protocolMap': self.protocolMapConfigs[id] = mod elif type == 'database': self.databaseConfigs[id] = mod elif type == '': msg = "Object must have a type attribute: %s" % id raise ConfigFileException(msg) elif mod.nodeType == elementType and mod.localName == "path": if (mod.hasAttributeNS(None, 'type') and mod.getAttributeNS( None, 'type') == 'includeConfigs'): # Import into our space if (mod.hasAttributeNS(None, 'ref')): # <path type="includeConfigs" ref="configStore"/> self._includeConfigStores.append( mod.getAttributeNS(None, 'ref')) else: # <path type="includeConfigs">path/to/file.xml</path> path = getFirstData(mod) # Expand user-specific paths path = os.path.expanduser(path) if not os.path.isabs(path): path = os.path.join( self.get_path(session, 'defaultPath'), path) if os.path.isdir(path): # include all configs in it at our space files = glob.glob("%s/*.xml" % path) for f in files: self._parseIncludes(session, f) else: self._parseIncludes(session, path) else: path = getFirstData(mod) # Expand user-specific paths path = os.path.expanduser(path) if not os.path.isabs(path): path = os.path.join( self.get_path(session, 'defaultPath'), path) dom = self._getDomFromFile(session, path) id = mod.getAttributeNS(None, 'id') self.subConfigs[id] = dom.childNodes[0] ot = mod.getAttributeNS(None, 'type') if ot == 'database': self.databaseConfigs[id] = dom.childNodes[0]
def makeObjectFromDom(session, topNode, parentObject): # Lots of indirections from xml to object objectType = None try: objectType = topNode.xpath('./objectType/text()')[0] except IndexError: # May have namespace try: objectType = topNode.xpath('./c3:objectType/text()', namespaces={'c3': CONFIG_NS})[0] except IndexError: from lxml import etree print etree.tostring(topNode) except AttributeError: # Not an Lxml config node for c in topNode.childNodes: if (c.nodeType == elementType and c.localName == "objectType"): # Here's what we want to instantiate objectType = getFirstData(c) break if objectType is None: raise (ConfigFileException('No objectType set in config file.')) else: objectType = objectType.strip() return buildObject(session, objectType, [topNode, parentObject])
def _handleLocationNode(self, session, child): data = {"maps": {}, "string": "", "type": ""} xp = getFirstData(child) data["string"] = xp if child.localName == "xpath": data["type"] = "xpath" else: try: data["type"] = child.getAttribute("type").lower() except: raise ConfigFileException("Location element in {0} must have " "'type' attribute".format(self.id)) if data["type"] == "xpath": for a in child.attributes.keys(): # ConfigStore using 4Suite if type(a) == tuple: attrNode = child.attributes[a] a = attrNode.name if a[:6] == "xmlns:": pref = a[6:] uri = child.getAttributeNS("http://www.w3.org/2000/xmlns/", pref) if not uri: uri = child.getAttribute(a) data["maps"][pref] = uri else: data[a] = child.getAttributeNS(None, a) return data
def makeObjectFromDom(session, topNode, parentObject): # Lots of indirections from xml to object objectType = None try: objectType = topNode.xpath('./objectType/text()')[0] except IndexError: # May have namespace try: objectType = topNode.xpath('./c3:objectType/text()', namespaces={'c3': CONFIG_NS})[0] except IndexError: from lxml import etree print etree.tostring(topNode) except AttributeError: # Not an Lxml config node for c in topNode.childNodes: if (c.nodeType == elementType and c.localName == "objectType"): # Here's what we want to instantiate objectType = getFirstData(c) break if objectType is None: raise(ConfigFileException('No objectType set in config file.')) else: objectType = objectType.strip() return buildObject(session, objectType, [topNode, parentObject])
def _handleLocationNode(self, session, child): data = {'maps': {}, 'string': '', 'type': ''} xp = getFirstData(child) data['string'] = xp if child.localName == 'xpath': data['type'] = 'xpath' else: try: data['type'] = child.getAttribute('type').lower() except: raise ConfigFileException("Location element in {0} must have " "'type' attribute".format(self.id)) if data['type'] == 'xpath': for a in child.attributes.keys(): # ConfigStore using 4Suite if type(a) == tuple: attrNode = child.attributes[a] a = attrNode.name if (a[:6] == "xmlns:"): pref = a[6:] uri = child.getAttributeNS('http://www.w3.org/2000/xmlns/', pref) if not uri: uri = child.getAttribute(a) data['maps'][pref] = uri else: data[a] = child.getAttributeNS(None, a) return data
def _handleConfigNode(self, session, node): if (node.nodeType == elementType and node.localName == 'relations'): self.relations = {} for rel in node.childNodes: if (rel.nodeType == elementType and rel.localName == 'relation'): relName = rel.getAttributeNS(None, 'name') fields = [] for fld in rel.childNodes: if fld.nodeType == elementType: if fld.localName == 'object': oid = getFirstData(fld) fields.append([oid, 'VARCHAR', oid]) elif fld.localName == 'field': fname = fld.getAttributeNS(None, 'name') ftype = getFirstData(fld) fields.append([fname, ftype, '']) self.relations[relName] = fields
def _walkNode(self, node): if node.localName == "action": # top node self.actionIdentifier = node.getAttributeNS(None, 'identifier') if not self.actionIdentifier: raise ConfigFileException("No action identifier") self.code = [" if "] for c in node.childNodes: if c.nodeType == elementType: self._walkNode(c) self.code.append(":") cstring = " ".join(self.code) fncode = '\n'.join([ "def handler(self, session, user):", cstring, " return True", " else:", " return False" ]) exec(fncode) setattr(self, 'hasPermission', MethodType(locals()['handler'], self, self.__class__)) elif node.localName in ["all", "any"]: if node.localName == "all": bool = "and" else: bool = "or" self.code.append("(") for c in node.childNodes: if c.nodeType == elementType: self._walkNode(c) self.code.append(bool) self.code.pop() self.code.append(")") elif node.localName == "flag": f = getFirstData(node) self.code.append("user.has_flag(session, \"%s\", object)" % f) elif node.localName == "environment": e = getFirstData(node) self.code.append("session.environment == \"%s\"" % e) elif node.localName == "hostmask": e = getFirstData(node) self.code.append("user.connectedFrom(session, \"%s\")" % e)
def _handleConfigNode(self, session, node): if (node.localName in self.simpleNodes): setattr(self, node.localName, getFirstData(node)) elif (node.localName == "flags"): # Extract Rights info # <flags> <flag> <object> <value> </flag> </flags> for c in node.childNodes: if c.nodeType == elementType and c.localName == "flag": obj = None flag = None for c2 in c.childNodes: if c2.nodeType == elementType: if c2.localName == "object": obj = getFirstData(c2) elif c2.localName == "value": flag = getFirstData(c2) if ( (flag not in self.allFlags) and (flag[:4] != "c3fn") ): msg = "Unknown flag: %s" % flag raise ConfigFileException(msg) if obj is None or flag is None: msg = ("Missing object or value element for flag for " "user %s" % self.username) raise ConfigFileException(msg) if (obj): f = self.flags.get(flag, []) if f != "": f.append(obj) self.flags[flag] = f else: self.flags[flag] = "" elif (node.localName == "history"): # Extract user history pass elif (node.localName == "hostmask"): # Extract allowed hostmask list pass
def _handleConfigNode(self, session, node): # Source if (node.localName == "xpath"): xpath = getFirstData(node) maps = {} for a in node.attributes.keys(): if (a[:6] == "xmlns:"): pref = a[6:] uri = node.getAttribute(a) maps[pref] = uri elif a == "type": tc = node.getAttribute(a) cxp = verifyXPaths([xpath]) if tc == 'copy': self.copyElems.append([cxp[0], maps]) else: self.tagElems.append([cxp[0], maps])
def __init__(self, session, config, parent=None): """Constructor inherited by all configured Cheshire3 objects. The constructor for all Cheshire3 objects take the same arguments: session: A Session object topNode: The <config> or <subConfig> domNode for the configuration parent: The object that provides the scope for this object. """ self.docstring = "" self.parent = parent self.subConfigs = CaselessDictionary() self.paths = {} self.objects = CaselessDictionary() self.settings = {} self.defaults = {} self.permissionHandlers = {} self.unresolvedObjects = {} self.functionLogger = None self._objectRefs = [] self._includeConfigStores = [] self.logger = None self.checkSums = {} self.pathCheckSums = {} self.version = "" self.complexity = "" self.stability = "" self.initTime = time.time() pathObjects = {} # LXML if hasattr(config, 'attrib'): self.id = config.attrib.get('id', '') self.version = config.attrib.get('version', '') self.complexity = config.attrib.get('complexity', '') self.stability = config.attrib.get('stability', '') walker = config.iterchildren(tag=etree.Element) for e in walker: if e.tag in ['name', '{%s}name' % CONFIG_NS]: self.name = e.text elif e.tag in ['objectType', '{%s}objectType' % CONFIG_NS]: self.objectType = e.text elif e.tag in ['checkSums', '{%s}checkSums' % CONFIG_NS]: for e2 in e.iterchildren(tag=etree.Element): # Store checksum on self, and hash code against it pt = e2.attrib.get('pathType', '__code__') ct = e2.attrib.get('type', 'md5') if pt != '__code__': try: self.pathCheckSums[pt].append((ct, e2.text)) except KeyError: self.pathCheckSums[pt] = [(ct, e2.text)] else: self.checkSums[ct] = e2.text elif e.tag in ['paths', '{%s}paths' % CONFIG_NS]: for e2 in e.iterchildren(tag=etree.Element): try: typ = e2.attrib['type'] except KeyError: raise ConfigFileException("path must have type") if e2.tag in ['path', '{%s}path' % CONFIG_NS]: # Allow template strings in paths # e.g. ${cheshire3Home}/foo/bar pathTmpl = Template(e2.text) sub = pathTmpl.safe_substitute self.paths[typ] = sub(cheshire3Paths) elif e2.tag in ['object', '{%s}object' % CONFIG_NS]: try: ref = e2.attrib['ref'] except KeyError: msg = "object must have ref" raise ConfigFileException(msg) pathObjects[typ] = ref elif e.tag in ['subConfigs', '{%s}subConfigs' % CONFIG_NS]: # Recurse self._recurseLxmlSubConfigs(session, e) elif e.tag in ['options', '{%s}options' % CONFIG_NS]: for e2 in e.iterchildren(tag=etree.Element): try: typ = e2.attrib['type'] except KeyError: msg = "option (setting/default) must have type" raise ConfigFileException(msg) if e2.tag in ['setting', '{%s}setting' % CONFIG_NS]: value = self._verifySetting(typ, e2.text) self.settings[typ] = value elif e2.tag in ['default', '{%s}default' % CONFIG_NS]: value = self._verifyDefault(typ, e2.text) self.defaults[typ] = value elif e.tag in ['actions', '{%s}actions' % CONFIG_NS]: pass elif e.tag in ['docs', '{%s}docs' % CONFIG_NS]: self.docstring = e.text else: self._handleLxmlConfigNode(session, e) del walker else: if (config.hasAttributeNS(None, 'id')): self.id = config.getAttributeNS(None, 'id') for child in config.childNodes: if child.nodeType == elementType: if child.localName == "name": self.name = getFirstData(child) elif (child.localName == "objectType"): self.objectType = getFirstData(child) elif (child.localName == "paths"): # Configure self with paths for child2 in child.childNodes: if child2.nodeType == elementType: type = child2.getAttributeNS(None, 'type') if child2.localName == "path": value = getFirstData(child2) # Allow template strings in paths # e.g. ${cheshire3Home}/foo/bar pathTmpl = Template(value) sub = pathTmpl.safe_substitute self.paths[type] = sub(cheshire3Paths) elif child2.localName == "object": value = child2.getAttributeNS(None, 'ref') pathObjects[type] = value elif (child.localName == "subConfigs"): # Pointers to dom nodes for config ids self._recurseSubConfigs(session, child) elif (child.localName == "objects"): for obj in child.childNodes: if ( obj.nodeType == elementType and obj.localName == "path" ): type = obj.getAttributeNS(None, 'type') id = obj.getAttributeNS(None, 'ref') self._objectRefs.append((id, type)) elif (child.localName == "options"): # See configInfo in ZeeRex for child2 in child.childNodes: if (child2.nodeType == elementType): type = child2.getAttributeNS(None, 'type') if (child2.localName == "setting"): dc = getFirstData(child2) if (dc): value = self._verifySetting(type, dc) self.settings[type] = value elif (child2.localName == "default"): dc = getFirstData(child2) if (dc): value = self._verifyDefault(type, dc) self.defaults[type] = value elif (child.localName == "actions"): # Permission rqmts for child2 in child.childNodes: if child2.nodeType == elementType: p = PermissionHandler(child2, self) self.permissionHandlers[p.actionIdentifier] = p elif (child.localName == "docs"): # Add per configuration documentation to docs stack. self.docstring = getFirstData(child) else: self._handleConfigNode(session, child) if ('pythonPath' in self.paths): sys.path.append(self.paths['pythonPath'][1]) # Allow any object to be set to debug # Functionality of this dependent on object self.debug = self.get_setting(session, "debug", 0) for p in self.permissionHandlers.keys(): if p[0:5] == 'c3fn:': self.add_auth(p[5:]) # Dynamically Instantiate objects. This is mindbending :} # Mindbending2: JIT building! if self.parent: self.parent.objects[self.id] = self for o in (self._objectRefs): # Instantiate obj = self.get_object(session, o[0]) # Add default Object types to paths for t in pathObjects.keys(): self.unresolvedObjects[t] = pathObjects[t] # Built, maybe set function logging log = self.get_setting(session, 'log', session.server.defaultFunctionLog) if log: fl = self.get_path(session, 'functionLogger') if fl != self: self.functionLogger = fl logList = log.strip().split() for l in logList: self.add_logging(session, l) try: del self.settings['log'] except KeyError: # from default pass # now checksum self if self.checkSums: code = inspect.getsource(self.__class__) for (ct, val) in self.checkSums.items(): m = hashlib.new(ct) m.update(code) digest = m.hexdigest() if digest != val: raise IntegrityException(self.id + ": " + digest) if self.pathCheckSums: # step through each referenced file and check for (pt, chk) in self.pathCheckSums.items(): for (ct, val) in chk: m = hashlib.new(ct) # read in file fn = self.get_path(session, pt) if not os.path.isabs(fn): if pt == 'executable': # search dp = self.get_path('session', 'executablePath', '') if not dp: dp = getShellResult('which {0}'.format(fn)) else: dp = self.get_path(session, 'defaultPath') fn = os.path.join(dp, fn) fh = file(fn) data = fh.read() fh.close() m.update(data) digest = m.hexdigest() if digest != val: msg = "%s/%s (%s): %s" % (self.id, pt, fn, digest) raise IntegrityException(msg)