def read(self, filename): 'Read in given .kdb file' fp = open(filename) buf = fp.read() fp.close() headbuf = buf[:124] self.header = DBHDR(headbuf) self.groups = [] self.entries = [] payload = buf[124:] # if self.masterkey: # key = self.masterkey # elif self.filekey or self.password: # key = self.composite_key() # self.finalkey = self.final_key(key, # self.header.final_master_seed, # self.header.transform_seed, # self.header.transform_rounds) self.finalkey = self.final_key() payload = self.decrypt_payload(payload, self.finalkey, self.header.encryption_type(), self.header.encryption_iv) ngroups = self.header.ngroups while ngroups: gi = GroupInfo(payload) self.groups.append(gi) length = len(gi) #print 'GroupInfo of length',length,'payload=',len(payload) payload = payload[length:] ngroups -= 1 continue nentries = self.header.nentries while nentries: ei = EntryInfo(payload) self.entries.append(ei) payload = payload[len(ei):] nentries -= 1 continue return
def read(self,filename): 'Read in given .kdb file' fp = open(filename) buf = fp.read() fp.close() headbuf = buf[:124] self.header = DBHDR(headbuf) self.groups = [] self.entries = [] payload = buf[124:] self.finalkey = self.final_key(self.masterkey, self.header.master_seed, self.header.master_seed2, self.header.key_enc_rounds) payload = self.decrypt_payload(payload, self.finalkey, self.header.encryption_type(), self.header.encryption_iv) ngroups = self.header.ngroups while ngroups: gi = GroupInfo(payload) self.groups.append(gi) length = len(gi) #print 'GroupInfo of length',length,'payload=',len(payload) payload = payload[length:] ngroups -= 1 continue nentries = self.header.nentries while nentries: ei = EntryInfo(payload) self.entries.append(ei) payload = payload[len(ei):] nentries -= 1 continue return
def main(): usage = """%prog [options] <kdb file> <command> Commands: list - List entries in file get <title> <key> - Get entry from file and display to stdout set <title> <key>=<value> [...] - Set entry key(s) to value(s) add <title> <key>=<value> [...] - Add entry and set key(s) to value(s). del <title> - Delete entry.""" parser = OptionParser(usage, version="%prog 1.0") parser.add_option("-p", "--passphrase", action="store", dest="passphrase", type="str", help="Passphrase to open kdb with. (Use 'ask' to be prompted) [%default]") parser.add_option("-k", "--keyfile", action="store", dest="keyfilename", type="str", help="Keyfile containg a key to open kdb with. [%default]") (options, args) = parser.parse_args() if len(args) < 2: parser.error("incorrect number of arguments") else: filename = args[0] command = args[1] if command == 'del' and len(args) != 3: parser.error("incorrect number of arguments for command 'add'") if command == 'get' and len(args) != 4: parser.error("incorrect number of arguments for command 'get'") if command == 'set' and len(args) < 4: parser.error("incorrect number of arguments for command 'set'") if command == 'add' and len(args) < 4: parser.error("incorrect number of arguments for command 'add'") if not os.access(filename, os.F_OK): if inputYN('Database does not exist. Create?', default='yes'): options.passphrase = create_database(filename, options) else: sys.exit(4) if options.passphrase == 'ask' or not (options.passphrase or options.keyfilename): options.passphrase = getpass() filekey = None if options.keyfilename: filekey = load_filekey(options.keyfilename) db = Database(filename, filekey=filekey, passphrase=options.passphrase) if command == 'list': print 'Groups:' for group in db.groups: print group.group_name print print 'Entries:' print("{0:20} {1:15} {2:20} {3:20}".format("Group", "Title", "Username", "URL")) for entry in db.entries: if hasattr(entry, 'title') and entry.title == 'Meta-Info': continue for group in db.groups: if hasattr(entry, 'groupid') and group.groupid == entry.groupid: break print("{0:<20} {1:15} {2:20} {3:20}".format(group.group_name, entry.title, entry.username, entry.url)) elif command == 'get': (title, key) = (args[2], args[3]) entry = db.get(title) print("{0}".format(getattr(entry, key))) elif command == 'set' or command == 'add': title = args[2] setpairs = {} for kp in args[3:]: (key, value) = kp.split("=") setpairs[key] = value if command == 'set': entry = db.get(title) elif command == 'add': entry = EntryInfo() entry.title = title db.entries.append(entry) for (key, value) in setpairs.items(): setattr(entry, key, value) if not hasattr(entry, 'groupid'): entry.groupid = 0 db.write() elif command == 'del': title = args[2] entry = db.get(title) db.entries.remove(entry) db.write()
def main(): usage = """%prog [options] <kdb file> <command> Commands: list - List entries in file get <title> <key> - Get entry from file and display to stdout set <title> <key>=<value> [...] - Set entry key(s) to value(s) add <title> <key>=<value> [...] - Add entry and set key(s) to value(s). del <title> - Delete entry.""" parser = OptionParser(usage, version="%prog 1.0") parser.add_option( "-p", "--passphrase", action="store", dest="passphrase", type="str", help= "Passphrase to open kdb with. (Use 'ask' to be prompted) [%default]") parser.add_option( "-k", "--keyfile", action="store", dest="keyfilename", type="str", help="Keyfile containg a key to open kdb with. [%default]") (options, args) = parser.parse_args() if len(args) < 2: parser.error("incorrect number of arguments") else: filename = args[0] command = args[1] if command == 'del' and len(args) != 3: parser.error("incorrect number of arguments for command 'add'") if command == 'get' and len(args) != 4: parser.error("incorrect number of arguments for command 'get'") if command == 'set' and len(args) < 4: parser.error("incorrect number of arguments for command 'set'") if command == 'add' and len(args) < 4: parser.error("incorrect number of arguments for command 'add'") if not os.access(filename, os.F_OK): if inputYN('Database does not exist. Create?', default='yes'): options.passphrase = create_database(filename, options) else: sys.exit(4) if options.passphrase == 'ask' or not (options.passphrase or options.keyfilename): options.passphrase = getpass() filekey = None if options.keyfilename: filekey = load_filekey(options.keyfilename) db = Database(filename, filekey=filekey, passphrase=options.passphrase) if command == 'list': print 'Groups:' for group in db.groups: print group.group_name print print 'Entries:' print("{0:20} {1:15} {2:20} {3:20}".format("Group", "Title", "Username", "URL")) for entry in db.entries: if hasattr(entry, 'title') and entry.title == 'Meta-Info': continue for group in db.groups: if hasattr(entry, 'groupid') and group.groupid == entry.groupid: break print("{0:<20} {1:15} {2:20} {3:20}".format( group.group_name, entry.title, entry.username, entry.url)) elif command == 'get': (title, key) = (args[2], args[3]) entry = db.get(title) print("{0}".format(getattr(entry, key))) elif command == 'set' or command == 'add': title = args[2] setpairs = {} for kp in args[3:]: (key, value) = kp.split("=") setpairs[key] = value if command == 'set': entry = db.get(title) elif command == 'add': entry = EntryInfo() entry.title = title db.entries.append(entry) for (key, value) in setpairs.items(): setattr(entry, key, value) if not hasattr(entry, 'groupid'): entry.groupid = 0 db.write() elif command == 'del': title = args[2] entry = db.get(title) db.entries.remove(entry) db.write()