def doReadKey(self, arg): # keyfn = arg['keyfilename'] src = arg['keysrc'] lg.out(2, 'installer.doReadKey length=%s' % len(src)) # src = bpio.ReadBinaryFile(keyfn) if len(src) > 1024 * 10: self.doPrint(('file is too big for private key', 'red')) return try: lines = src.splitlines() idurl = lines[0].strip() keysrc = '\n'.join(lines[1:]) if idurl != nameurl.FilenameUrl(nameurl.UrlFilename(idurl)): idurl = '' keysrc = src except: lg.exc() idurl = '' keysrc = src if self.state not in self.output: self.output[self.state] = {'data': [('', 'black')]} self.output[self.state] = {'data': [('', 'black')]} self.output[self.state]['idurl'] = idurl self.output[self.state]['keysrc'] = keysrc if 'RECOVER' not in self.output: self.output['RECOVER'] = {'data': [('', 'black')]} if keysrc and idurl: self.output['RECOVER']['data'].append( ('private key and IDURL was loaded', 'green')) elif not idurl and keysrc: self.output['RECOVER']['data'].append( ('private key was loaded, provide correct IDURL now', 'blue'))
def doPasteKey(self, arg): src = misc.getClipboardText() try: lines = src.split('\n') idurl = lines[0] keysrc = '\n'.join(lines[1:]) if idurl != nameurl.FilenameUrl(nameurl.UrlFilename(idurl)): idurl = '' keysrc = src except: lg.exc() idurl = '' keysrc = src if self.state not in self.output: self.output[self.state] = {'data': [('', 'black')]} self.output[self.state]['idurl'] = idurl self.output[self.state]['keysrc'] = keysrc if 'RECOVER' not in self.output: self.output['RECOVER'] = {'data': [('', 'black')]} if keysrc and idurl: self.output['RECOVER']['data'].append( ('private key and IDURL was loaded', 'green')) elif not idurl and keysrc: self.output['RECOVER']['data'].append( ('private key was loaded, provide correct IDURL now', 'blue'))
def get(url): """ A smart way to get identity from cache. If not cached in memory but found on disk - will cache from disk. """ if has_idurl(url): return idget(url) else: try: partfilename = nameurl.UrlFilename(url) except: lg.out(1, "identitydb.get ERROR %s is incorrect" % str(url)) return None filename = os.path.join(settings.IdentityCacheDir(), partfilename) if not os.path.exists(filename): lg.out( 6, "identitydb.get file %s not exist" % os.path.basename(filename)) return None idxml = bpio.ReadTextFile(filename) if idxml: idobj = identity.identity(xmlsrc=idxml) url2 = idobj.getIDURL() if url == url2: idset(url, idobj) return idobj else: lg.out(1, "identitydb.get ERROR url=%s url2=%s" % (url, url2)) return None lg.out(6, "identitydb.get %s not found" % nameurl.GetName(url)) return None
def send(customer_idurl, packet_id, format_type): customer_name = nameurl.GetName(customer_idurl) MyID = my_id.getLocalID() RemoteID = customer_idurl PacketID = packet_id if _Debug: lg.out( _DebugLevel, "list_files.send to %s, format is '%s'" % (customer_name, format_type)) custdir = settings.getCustomersFilesDir() ownerdir = os.path.join(custdir, nameurl.UrlFilename(customer_idurl)) if not os.path.isdir(ownerdir): if _Debug: lg.out(_DebugLevel, "list_files.send did not found customer dir: " + ownerdir) src = PackListFiles('', format_type) result = signed.Packet(commands.Files(), MyID, MyID, PacketID, src, RemoteID) gateway.outbox(result) return result plaintext = TreeSummary(ownerdir) if _Debug: lg.out(_DebugLevel + 8, '\n%s' % (plaintext)) src = PackListFiles(plaintext, format_type) result = signed.Packet(commands.Files(), MyID, MyID, PacketID, src, RemoteID) gateway.outbox(result) return result
def get_filename(idurl): try: partfilename = nameurl.UrlFilename(idurl) except: lg.out(1, "identitydb.get_filename ERROR %s is incorrect" % str(idurl)) return None return os.path.join(settings.IdentityCacheDir(), partfilename)
def get_filename(idurl): idurl = id_url.to_original(idurl) try: partfilename = nameurl.UrlFilename(idurl) except: lg.err("idurl %r is not correct" % idurl) return None return os.path.join(settings.IdentityCacheDir(), partfilename)
def has_file(idurl): """ """ try: partfilename = nameurl.UrlFilename(idurl) except: lg.out(1, "identitydb.has_file ERROR %s is not correct" % str(idurl)) return None filename = os.path.join(settings.IdentityCacheDir(), partfilename) return os.path.exists(filename)
def update(idurl, xml_src): """ This is a correct method to update an identity in the local cache. PREPRO need to check that date or version is after old one so not vulnerable to replay attacks. """ idurl = id_url.to_original(idurl) try: newid = identity.identity(xmlsrc=xml_src) except: lg.exc() return False if not newid.isCorrect(): lg.err("incorrect identity : %r" % idurl) return False try: if not newid.Valid(): lg.err("identity not valid : %r" % idurl) return False except: lg.exc() return False filename = os.path.join(settings.IdentityCacheDir(), nameurl.UrlFilename(idurl)) if os.path.exists(filename): oldidentityxml = bpio.ReadTextFile(filename) oldidentity = identity.identity(xmlsrc=oldidentityxml) if oldidentity.publickey != newid.publickey: # TODO: SECURITY add some kind of black list to be able to block certain IP's if the DDoS me lg.err( "new public key does not match with old, SECURITY VIOLATION : %r" % idurl) return False if oldidentity.signature != newid.signature: if _Debug: lg.out( _DebugLevel, 'identitydb.update have new data for %r' % nameurl.GetName(idurl)) else: idset(idurl, newid) return True # publickeys match so we can update it bpio.WriteTextFile(filename, xml_src) idset(idurl, newid) return True
def update(idurl, xml_src): """ This is a correct method to update an identity in the local cache. PREPRO need to check that date or version is after old one so not vulnerable to replay attacks. """ idurl = strng.to_bin(idurl) try: newid = identity.identity(xmlsrc=xml_src) except: lg.exc() return False if not newid.isCorrect(): lg.out(1, "identitydb.update ERROR incorrect identity : %r" % idurl) return False try: if not newid.Valid(): lg.out(1, "identitydb.update ERROR identity not Valid : %r" % idurl) return False except: lg.exc() return False filename = os.path.join(settings.IdentityCacheDir(), nameurl.UrlFilename(idurl)) if os.path.exists(filename): oldidentityxml = bpio.ReadTextFile(filename) oldidentity = identity.identity(xmlsrc=oldidentityxml) if oldidentity.publickey != newid.publickey: lg.out( 1, "identitydb.update ERROR new publickey does not match old, SECURITY VIOLATION : %r" % idurl) return False if oldidentity.signature != newid.signature: lg.out( 6, 'identitydb.update have new data for %r' % nameurl.GetName(idurl)) else: idset(idurl, newid) return True # publickeys match so we can update it bpio.WriteTextFile(filename, xml_src) idset(idurl, newid) return True
def remove(url): """ Top method to remove identity from cache - also remove local file. """ filename = os.path.join(settings.IdentityCacheDir(), nameurl.UrlFilename(url)) if os.path.isfile(filename): lg.out(6, "identitydb.remove file %s" % filename) try: os.remove(filename) except: lg.exc() idremove(url)
def ListCustomerFiles1(customerNumber): """ On the status form when clicking on a customer, find out what files we're holding for that customer. """ idurl = contactsdb.customer(customerNumber) filename = nameurl.UrlFilename(idurl) customerDir = os.path.join(settings.getCustomersFilesDir(), filename) if os.path.exists(customerDir) and os.path.isdir(customerDir): backupFilesList = os.listdir(customerDir) if len(backupFilesList) > 0: return ListSummary(backupFilesList) return "No files stored for this customer"
def remove(idurl): """ Top method to remove identity from cache - also remove local file. """ idurl = id_url.to_original(idurl) filename = os.path.join(settings.IdentityCacheDir(), nameurl.UrlFilename(idurl)) if os.path.isfile(filename): if _Debug: lg.out(_DebugLevel, "identitydb.remove file %r" % filename) try: os.remove(filename) except: lg.exc() idremove(idurl) return True
def ListCustomerFiles(customer_idurl): filename = nameurl.UrlFilename(customer_idurl) customer_dir = os.path.join(settings.getCustomersFilesDir(), filename) result = cStringIO.StringIO() def cb(realpath, subpath, name): if os.path.isdir(realpath): result.write('D%s\n' % subpath) else: result.write('F%s\n' % subpath) return True bpio.traverse_dir_recursive(cb, customer_dir) src = result.getvalue() result.close() return src
def constructFilename(customerIDURL, packetID): customerGlobID, packetID = packetid.SplitPacketID(packetID) if customerGlobID: customerIDURL_packet = global_id.GlobalUserToIDURL(customerGlobID) if customerIDURL_packet != customerIDURL: lg.warn('construct filename for another customer: %s != %s' % (customerIDURL_packet, customerIDURL)) customerDirName = nameurl.UrlFilename(customerIDURL) customersDir = settings.getCustomersFilesDir() if not os.path.exists(customersDir): bpio._dir_make(customersDir) ownerDir = os.path.join(customersDir, customerDirName) if not os.path.exists(ownerDir): bpio._dir_make(ownerDir) filename = os.path.join(ownerDir, packetID) return filename
def get_ident(idurl): """ A smart way to get identity from cache. If not cached in memory but found locally - read it from disk. """ idurl = id_url.to_original(idurl) if has_idurl(idurl): return idget(idurl) try: partfilename = nameurl.UrlFilename(idurl) except: if _Debug: lg.out(_DebugLevel, "identitydb.get_ident ERROR %r is incorrect" % idurl) return None if not partfilename: if _Debug: lg.out(_DebugLevel, "identitydb.get_ident ERROR %r is empty" % idurl) return None filename = os.path.join(settings.IdentityCacheDir(), partfilename) if not os.path.exists(filename): if _Debug: lg.out( _DebugLevel, "identitydb.get_ident file %r not exist" % os.path.basename(filename)) return None idxml = bpio.ReadTextFile(filename) if not idxml: if _Debug: lg.out( _DebugLevel, "identitydb.get_ident %s not found" % nameurl.GetName(idurl)) return None idobj = identity.identity(xmlsrc=idxml) idurl_orig = idobj.getIDURL() if idurl == idurl_orig.original(): idset(idurl, idobj) return idobj lg.err("not found identity object idurl=%r idurl_orig=%r" % (idurl, idurl_orig)) return None
# This file (treesummary.py) is part of BitDust Software. # # BitDust is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # BitDust Software is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with BitDust Software. If not, see <http://www.gnu.org/licenses/>. # # Please contact us if you have any questions at [email protected] import os import sys import os.path as _p sys.path.insert(0, _p.abspath(_p.join(_p.dirname(_p.abspath(sys.argv[0])), '..'))) from logs import lg from system import bpio from p2p import p2p_service from main import settings from lib import nameurl custdir = settings.getCustomersFilesDir() ownerdir = os.path.join(custdir, nameurl.UrlFilename('http://megafaq.ru/e_vps1004.xml')) plaintext = p2p_service.TreeSummary(ownerdir) print plaintext
def rating_dir(idurl): return os.path.join(settings.RatingsDir(), nameurl.UrlFilename(idurl))