From: <Z3...@us...> - 2012-10-14 17:40:24
|
Revision: 347 http://spd.svn.sourceforge.net/spd/?rev=347&view=rev Author: Z3po Date: 2012-10-14 17:40:17 +0000 (Sun, 14 Oct 2012) Log Message: ----------- added gpg support. Mostly copied functionality from current trunk to spd-ng...need to look into that. spdConfig Module needs to be redone....don't know why i thought it might be cool that way o.0 Modified Paths: -------------- branches/spd-ng/src/spdConfig.py branches/spd-ng/src/spdCore.py branches/spd-ng/src/spdInterface.py branches/spd-ng/src/spdInterfaces/cli.py branches/spd-ng/src/spdStore/devel.py branches/spd-ng/src/spdStore/plain.py Added Paths: ----------- branches/spd-ng/src/spdStore/gpg.py Modified: branches/spd-ng/src/spdConfig.py =================================================================== --- branches/spd-ng/src/spdConfig.py 2012-10-07 14:08:40 UTC (rev 346) +++ branches/spd-ng/src/spdConfig.py 2012-10-14 17:40:17 UTC (rev 347) @@ -17,6 +17,7 @@ ReturnCodes = { 1 : 'Missing ConfigFile', 2 : 'Section not found', 4 : 'Option not found' } + ConfigSection = 'Main' def __init__(self, configfile='~/.spd/config'): # {{{ @@ -69,7 +70,7 @@ config.write(configfile) # }}} - def __hasSection(self,section): # {{{ + def __hasSection(self, section): # {{{ """Check if a given section exists. # section: the given section.""" @@ -83,7 +84,7 @@ return 2, config # }}} - def __addSection(self,section): # {{{ + def __addSection(self, section): # {{{ """create a section if its not in configfile. # section: the section to create.""" @@ -99,7 +100,7 @@ return 0 # }}} - def __delSection(self,section): # {{{ + def __delSection(self, section): # {{{ """delete a Section from configfile. # section: the section to delete.""" @@ -115,7 +116,7 @@ return 0 # }}} - def __getOption(self,section,option): # {{{ + def getOption(self, section, option): # {{{ """Return the option from section. # section: in which section is the option configured? # option: option to return.""" @@ -131,7 +132,7 @@ return 4, None # }}} - def __setOption(self,section,option,value): # {{{ + def setOption(self, section, option, value): # {{{ """Create or set the given option. # section: section of the option. # option: option to set/change. @@ -142,7 +143,7 @@ if reval > 0: return reval - config.set(section,option,value) + config.set(section, option, value) reval = self.__writeConfigFile(config) if reval > 0: @@ -151,7 +152,7 @@ return 0 # }}} - def __delOption(self,section,option): # {{{ + def delOption(self, section, option): # {{{ """delete an option. # section: which section holds the option? # option: option to delete.""" @@ -161,7 +162,7 @@ if reval > 0: return reval - reval, option = self.__getOption(section,option) + reval, option = self.getOption(section,option) if reval > 0: return reval @@ -201,7 +202,7 @@ # }}} - def addConfigSection(self,section): # {{{ + def addConfigSection(self, section): # {{{ reval = self.__addSection(section) if reval > 0: @@ -211,7 +212,7 @@ def getConfigVersion(self, raw=False): # {{{ """Return the config version.""" - reval, option = self.__getOption("Main","version") + reval, option = self.getOption(self.ConfigSection,"version") if reval > 1 and reval < 2: raise KeyError(self.ReturnCodes[reval]) # don't tolerate These @@ -228,11 +229,11 @@ return option # }}} - def setConfigVersion(self,version): # {{{ + def setConfigVersion(self, version): # {{{ """set the config version to the given version. # version: version number.""" - reval = self.__setOption("Main","version",version) + reval = self.setOption(self.ConfigSection,"version",version) if reval > 0: raise KeyError(self.ReturnCodes[reval]) @@ -242,7 +243,7 @@ '''get the clientmode. # return the clientmode option''' - reval, clientmode = self.__getOption('Main', 'clientmode') + reval, clientmode = self.getOption(self.ConfigSection, 'clientmode') if reval > 0 and reval < 4: raise KeyError(self.ReturnCodes[reval]) @@ -259,30 +260,27 @@ return clientmode # }}} - def setClientmode(self,clientmode): # {{{ + def setClientmode(self, clientmode): # {{{ """set the clientmode. # clientmode: clientmode to use.""" - reval = self.__setOption("Main","clientmode",clientmode) + reval = self.setOption(self.ConfigSection,"clientmode",clientmode) if reval > 0: raise KeyError(self.ReturnCodes[reval]) # }}} - def getPasswordfield(self, raw=False): # {{{ + def getPasswordfield(self, section=ConfigSection, raw=False): # {{{ '''get the clientmode. # return the Password Column''' - reval, clientmode = self.__getOption('Main', 'passwordfield') + reval, clientmode = self.getOption(section, 'passwordfield') if reval > 0 and reval < 4: raise KeyError(self.ReturnCodes[reval]) if reval > 0: - if raw: return reval, None - else: - raise KeyError(self.ReturnCodes[reval]) if raw: return reval, clientmode @@ -290,21 +288,21 @@ return clientmode # }}} - def setPasswordfield(self,passcolumn): # {{{ + def setPasswordfield(self, passcolumn, section=ConfigSection): # {{{ """set the Password Column. # passcolumn: passcolumn to set.""" - reval = self.__setOption("Main","passwordfield",passcolumn) + reval = self.setOption(section,"passwordfield",passcolumn) if reval > 0: raise KeyError(self.ReturnCodes[reval]) # }}} - def getStoretype(self, raw=False): # {{{ + def getStoretype(self, raw=False, section=ConfigSection): # {{{ '''get the clientmode. # return the clientmode option''' - reval, storetype = self.__getOption('Main', 'storetype') + reval, storetype = self.getOption(section, 'storetype') if reval > 0 and reval < 4: raise KeyError(self.ReturnCodes[reval]) @@ -321,24 +319,18 @@ return storetype # }}} - def getPasswordfile(self, raw=False): # {{{ + def getPasswordfile(self, section=ConfigSection): # {{{ '''get the password field. # return the password field''' - reval, storetype = self.__getOption('Main', 'file') + reval, storetype = self.getOption(section, 'file') - if reval > 0 and reval < 4: + if reval > 0: raise KeyError(self.ReturnCodes[reval]) if reval > 0: - if raw: - return reval, None - else: raise KeyError(self.ReturnCodes[reval]) - if raw: - return reval, storetype - return storetype # }}} Modified: branches/spd-ng/src/spdCore.py =================================================================== --- branches/spd-ng/src/spdCore.py 2012-10-07 14:08:40 UTC (rev 346) +++ branches/spd-ng/src/spdCore.py 2012-10-14 17:40:17 UTC (rev 347) @@ -22,11 +22,15 @@ # create the database from the stuff out of the store def __init__(self,ConfigObject): # {{{ + self.ConfigObject = ConfigObject self.PasswordPreset = ConfigObject.ConfigSection self.PasswordColumn = ConfigObject.getPasswordfield() self.PasswordFile = ConfigObject.getPasswordfile() self.spdStore = __import__('spdStore.' + ConfigObject.getStoretype(), fromlist=['',]) - self.db = self.spdStore.getData(self.PasswordFile, self.PasswordPreset) + # }}} + + def fetchData(self): # {{{ + self.db = self.spdStore.getData(self.ConfigObject) self.db.row_factory = self.__sql2dict self.dbc = self.db.cursor() # }}} @@ -41,7 +45,7 @@ def saveData(self): # {{{ if self.spdStore != None: - self.spdStore.saveData(self.PasswordFile, self.PasswordPreset, self.db) + self.spdStore.saveData(self.ConfigObject, self.db) # }}} def searchEntry(self,SearchList,byid=False): # {{{ Modified: branches/spd-ng/src/spdInterface.py =================================================================== --- branches/spd-ng/src/spdInterface.py 2012-10-07 14:08:40 UTC (rev 346) +++ branches/spd-ng/src/spdInterface.py 2012-10-14 17:40:17 UTC (rev 347) @@ -106,6 +106,10 @@ self.core.saveData() # }}} + def fetchData(self, __blackhole='nothing'): # {{{ + self.core.fetchData() + # }}} + def printPasswords(self,args): # {{{ passdict = self.__findPasswords(args) self.client.printPasswords(passdict, args) Modified: branches/spd-ng/src/spdInterfaces/cli.py =================================================================== --- branches/spd-ng/src/spdInterfaces/cli.py 2012-10-07 14:08:40 UTC (rev 346) +++ branches/spd-ng/src/spdInterfaces/cli.py 2012-10-14 17:40:17 UTC (rev 347) @@ -266,20 +266,20 @@ 'pwonly' : options.pwonly }) if options.addEntry: - return [("addEntry","saveData"), __optionsdict] + return [("fetchData","addEntry","saveData"), __optionsdict] elif options.deleteEntry: - return [("deleteEntry","saveData"), __optionsdict] + return [("fetchData","deleteEntry","saveData"), __optionsdict] elif options.editEntry: - return [("editEntry","saveData"), __optionsdict] + return [("fetchData","editEntry","saveData"), __optionsdict] elif options.deleteColumn: - return [("deleteColumn","saveData"), __optionsdict] + return [("fetchData","deleteColumn","saveData"), __optionsdict] elif options.renameColumn: - return [("renameColumn","saveData"), __optionsdict] + return [("fetchData","renameColumn","saveData"), __optionsdict] elif options.addColumn: - return [("addColumn","saveData"), __optionsdict] + return [("fetchData","addColumn","saveData"), __optionsdict] - return ["printPasswords", __optionsdict] + return [("fetchData","printPasswords"), __optionsdict] # }}} # EOF Modified: branches/spd-ng/src/spdStore/devel.py =================================================================== --- branches/spd-ng/src/spdStore/devel.py 2012-10-07 14:08:40 UTC (rev 346) +++ branches/spd-ng/src/spdStore/devel.py 2012-10-14 17:40:17 UTC (rev 347) @@ -9,14 +9,14 @@ This module is just for development. No real data should be written into that database''' import sqlite3 +from os import path as ospath -testdb = '/home/scabrera/Projects/spd/branches/spd-ng/src/test.db' - -def getData(passwordfile, passwordpreset): # {{{ - db = sqlite3.connect(testdb) +def getData(ConfigObject): # {{{ + passwordfile = ConfigObject.getPasswordfile() + db = sqlite3.connect(ospath.expanduser(passwordfile)) return db # }}} -def saveData(passwordfile, passwordpreset, database): # {{{ +def saveData(ConfigObject, database): # {{{ pass # }}} Copied: branches/spd-ng/src/spdStore/gpg.py (from rev 345, branches/spd-ng/src/spdStore/plain.py) =================================================================== --- branches/spd-ng/src/spdStore/gpg.py (rev 0) +++ branches/spd-ng/src/spdStore/gpg.py 2012-10-14 17:40:17 UTC (rev 347) @@ -0,0 +1,136 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2009 +# Klaus Umbach <tre...@us...> +# Sebastian Cabrera <z3...@us...> +'''This storage module handles plaintext files seperated by "seperator"''' + +import sqlite3 +from os import path as ospath +from pyme import core, callbacks +from re import sub as resub + +separator = '\t' + +def getSQLData(passwordpreset, sqlitedb): # {{{ + sqlitecursor = sqlitedb.cursor() + + query = "pragma Table_info('" + passwordpreset + "')" + sqlitecursor.execute(query) + + columns = [] + for i in sqlitecursor: + if i['name'] == "__ID": + continue + columns.append(str(i['name'])) + + query = "SELECT * FROM " + passwordpreset + sqlitecursor.execute(query) + data = sqlitecursor.fetchall() + + return columns, data + # }}} + +def getData(ConfigObject): #{{{ + """This function reads the password file""" + + passwordfile = ConfigObject.getPasswordfile() + passwordpreset = ConfigObject.ConfigSection + + enc = core.Data(file=ospath.expanduser(passwordfile)) + dec = core.Data() + c = core.Context() + + c.set_passphrase_cb(callbacks.passphrase_stdin, 'for decryption.') + c.op_decrypt(enc,dec) + dec.seek(0,0) + + # make fobj "something" like a file + passfile = dec.read().split('\n') + del passfile[len(passfile)-1] + + sqlitedb = sqlite3.connect(":memory:") + sqlitecursor = sqlitedb.cursor() + query = "CREATE TABLE " + passwordpreset + " ( __ID INTEGER PRIMARY KEY," + + for linecount, i in enumerate(passfile): + i = resub('\'','\'\'\'',i) + line = i.strip('\n').split(separator) + + if linecount == 0: # the first line defines the collumns + for column in line: + query += '`' + column + "` TEXT, " + query = query[0:-2] + ' )' + sqlitecursor.execute(query) + + query = "INSERT INTO " + passwordpreset + " ( " + for column in line: + query += column + ', ' + query = query[0:-2] + ' ) VALUES ( \'' + continue + + inserts = "" + for column in line: + inserts += column + '\', \'' + inserts = inserts[0:-4] + '\' )' + sqlitecursor.execute(query + inserts) + + sqlitedb.commit() + del passfile + + return sqlitedb +#}}} + +def saveData(ConfigObject, sqlitedb): # {{{ + + gpgrecipients = [] + reval = 0 + counter = 1 + passwordpreset = ConfigObject.ConfigSection + passwordfile = ConfigObject.getPasswordfile() + + reval, gpgrecipient = ConfigObject.getOption(passwordpreset, 'gpguser' + str(counter)) + + while gpgrecipient != None: + print str(counter) + print str(gpgrecipient) + counter += 1 + gpgrecipients.append(gpgrecipient) + reval, gpgrecipient = ConfigObject.getOption(passwordpreset, 'gpguser' + str(counter)) + + columns, newdata = getSQLData(passwordpreset, sqlitedb) + + insertdata = "" + + for column in columns: + insertdata += column.encode("utf-8") + '\t' + insertdata = insertdata[0:-1] + '\n' + + for datarow in newdata: + for column in columns: + insertdata += datarow[column].encode("utf-8") + '\t' + insertdata = insertdata[0:-1] + '\n' + + plaindata = core.Data(insertdata) + cipher = core.Data() + c = core.Context() + c.set_armor(1) + + # Get the first keys, it finds + RecipientKeys = [] + for i in gpgrecipients: + c.op_keylist_start(i,0) + RecipientKeys.append(c.op_keylist_next()) + + # encrypt with RecipientKeys + + c.op_encrypt(RecipientKeys, 1, plaindata, cipher) + cipher.seek(0,0) + + gpgfile = open(ospath.expanduser(passwordfile),"w") + gpgfile.write(cipher.read()) + gpgfile.close() + +# }}} + +# vim:filetype=python:foldmethod=marker:autoindent:expandtab:tabstop=4 Modified: branches/spd-ng/src/spdStore/plain.py =================================================================== --- branches/spd-ng/src/spdStore/plain.py 2012-10-07 14:08:40 UTC (rev 346) +++ branches/spd-ng/src/spdStore/plain.py 2012-10-14 17:40:17 UTC (rev 347) @@ -6,6 +6,7 @@ '''This storage module handles plaintext files seperated by "seperator"''' import sqlite3 +from os import path as ospath separator = '\t' @@ -28,11 +29,13 @@ return columns, data # }}} - -def getData(passwordfile, passwordpreset): #{{{ +def getData(ConfigObject): #{{{ """This function reads the password file""" - passfile = open(passwordfile,"r") + passwordfile = ConfigObject.getPasswordfile() + passwordpreset = ConfigObject.ConfigSection + + passfile = open(ospath.expanduser(passwordfile),"r") sqlitedb = sqlite3.connect(":memory:") sqlitecursor = sqlitedb.cursor() query = "CREATE TABLE " + passwordpreset + " ( __ID INTEGER PRIMARY KEY," @@ -64,7 +67,11 @@ return sqlitedb #}}} -def saveData(passwordfile, passwordpreset, sqlitedb): # {{{ +def saveData(ConfigObject, sqlitedb): # {{{ + + passwordfile = ConfigObject.getPasswordfile() + passwordpreset = ConfigObject.ConfigSection + passfile = open(passwordfile,"w") columns, newdata = getSQLData(passwordpreset, sqlitedb) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |