-rw-r--r-- | pwmanager/pwmanager/randomizer.cpp | 2 | ||||
-rw-r--r-- | pwmanager/pwmanager/randomizer.h | 2 | ||||
-rw-r--r-- | pwmanager/pwmanager/serializer.cpp | 4 | ||||
-rw-r--r-- | pwmanager/pwmanager/serializer.h | 2 |
4 files changed, 5 insertions, 5 deletions
diff --git a/pwmanager/pwmanager/randomizer.cpp b/pwmanager/pwmanager/randomizer.cpp index 1269c53..e1085ff 100644 --- a/pwmanager/pwmanager/randomizer.cpp +++ b/pwmanager/pwmanager/randomizer.cpp @@ -1,221 +1,221 @@ /*************************************************************************** * * * copyright (C) 2003, 2004 by Michael Buesch * * email: mbuesch@freenet.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License version 2 * * as published by the Free Software Foundation. * * * ***************************************************************************/ /*************************************************************************** * copyright (C) 2004 by Ulf Schenk - * This file is originaly based on version 2.0 of pwmanager + * This file is originaly based on version 1.1 of pwmanager * and was modified to run on embedded devices that run microkde * * $Id$ **************************************************************************/ #include "randomizer.h" #include "pwmexception.h" #include <qfile.h> #include <kapplication.h> #include <stdlib.h> #include <time.h> #ifdef PWM_EMBEDDED #ifndef Q_LONG #define Q_LONG long #endif #endif //PWM_EMBEDDED Randomizer * Randomizer::rndObj (0); Randomizer::Randomizer() { rndDev = new QFile; seed = time(0); #if 1 // set to 0 to test rand_r() fallback // probe for /dev/urandom rndDev->setName("/dev/urandom"); if (rndDev->exists() && rndDev->open(IO_ReadOnly)) { printDebug("Randomizer: using /dev/urandom"); return; } // probe for /dev/random rndDev->setName("/dev/random"); if (rndDev->exists() && rndDev->open(IO_ReadOnly)) { printDebug("Randomizer: using /dev/random"); return; } // probe for EGD char *fn = getenv("RANDFILE"); if (fn) { rndDev->setName(fn); if (rndDev->exists() && rndDev->open(IO_ReadOnly)) { printDebug(string("Randomizer: using $RANDFILE \"") + fn + "\" (aka EGD)"); return; } } #endif /* no secure randomizer found. * Fall back to stdlib randomizer. */ delete_and_null(rndDev); printWarn("neither /dev/*random nor EGD found! " "Falling back to insecure rand_r()!"); } Randomizer::~Randomizer() { #ifndef PWM_EMBEDDED while (mutex.locked()) { /* wait for the mutex to unlock. * Don't block the GUI here, so processEvents() */ kapp->processEvents(); } #endif if (rndDev) { rndDev->close(); delete rndDev; } } char Randomizer::genRndChar() { char ret; #ifndef PWM_EMBEDDED mutex.lock(); #endif if (rndDev) { /* we have a file which provides random data. * Simply read it. */ ret = rndDev->getch(); } else { /* fall back to rand_r() */ ret = rand_r(&seed) % 0xFF; } #ifndef PWM_EMBEDDED mutex->unlock(); #endif return ret; } int Randomizer::genRndInt() { int ret; #ifndef PWM_EMBEDDED mutex->lock(); #endif if (rndDev) { if (sizeof(int) == 4) { (reinterpret_cast<char *>(&ret))[0] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[1] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[2] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[3] = rndDev->getch(); } else if (sizeof(int) == 8) { (reinterpret_cast<char *>(&ret))[0] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[1] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[2] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[3] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[4] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[5] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[6] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[7] = rndDev->getch(); } else { printWarn(string(__FILE__) + ":" + tostr(__LINE__) + ": sizeof(int) != 4 && sizeof(int) != 8"); ret = rand_r(&seed); } } else { ret = rand_r(&seed); } #ifndef PWM_EMBEDDED mutex->unlock(); #endif return ret; } unsigned int Randomizer::genRndUInt() { unsigned int ret; #ifndef PWM_EMBEDDED mutex->lock(); #endif if (rndDev) { if (sizeof(unsigned int) == 4) { (reinterpret_cast<char *>(&ret))[0] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[1] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[2] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[3] = rndDev->getch(); } else if (sizeof(unsigned int) == 8) { (reinterpret_cast<char *>(&ret))[0] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[1] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[2] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[3] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[4] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[5] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[6] = rndDev->getch(); (reinterpret_cast<char *>(&ret))[7] = rndDev->getch(); } else { printWarn(string(__FILE__) + ":" + tostr(__LINE__) + ": sizeof(unsigned int) != 4 && sizeof(unsigned int) != 8"); ret = rand_r(&seed); } } else { ret = rand_r(&seed); } #ifndef PWM_EMBEDDED mutex->unlock(); #endif return ret; } void Randomizer::genRndBuf(unsigned char *buf, size_t len) { #ifndef PWM_EMBEDDED mutex->lock(); #endif if (rndDev) { Q_LONG n; n = rndDev->readBlock(reinterpret_cast<char *>(buf), len); WARN_ON(n != static_cast<Q_LONG>(len)); } else { size_t i; for (i = 0; i < len; ++i) buf[i] = rand_r(&seed) % 0xFF; } #ifndef PWM_EMBEDDED mutex->unlock(); #endif } string Randomizer::genRndBuf(size_t len) { string ret; unsigned char *buf; buf = new unsigned char[len]; genRndBuf(buf, len); ret.assign(reinterpret_cast<const char *>(buf), len); return ret; } diff --git a/pwmanager/pwmanager/randomizer.h b/pwmanager/pwmanager/randomizer.h index 5eb02f1..f2a6015 100644 --- a/pwmanager/pwmanager/randomizer.h +++ b/pwmanager/pwmanager/randomizer.h @@ -1,86 +1,86 @@ /*************************************************************************** * * * copyright (C) 2003, 2004 by Michael Buesch * * email: mbuesch@freenet.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License version 2 * * as published by the Free Software Foundation. * * * ***************************************************************************/ /*************************************************************************** * copyright (C) 2004 by Ulf Schenk - * This file is originaly based on version 2.0 of pwmanager + * This file is originaly based on version 1.1 of pwmanager * and was modified to run on embedded devices that run microkde * * $Id$ **************************************************************************/ #ifndef __RANDOMIZER_H #define __RANDOMIZER_H #include "pwmexception.h" #ifndef PWM_EMBEDDED #include <qmutex.h> #endif #include <string> using std::string; class QFile; /** Randomizer to get random values. * This class is thread-safe. * You should always use the instance returned by * obj() to use it. */ class Randomizer { public: Randomizer(); ~Randomizer(); static Randomizer * obj() { PWM_ASSERT(rndObj); return rndObj; } static void init() { PWM_ASSERT(!rndObj); rndObj = new Randomizer; } static void cleanup() { delete_ifnot_null(rndObj); } /** generate random char */ char genRndChar(); /** generate random int */ int genRndInt(); /** generate a random unsigned int */ unsigned int genRndUInt(); /** returns a buffer with random data */ string genRndBuf(size_t len); /** returns a buffer with random data */ void genRndBuf(unsigned char *buf, size_t len); protected: /** random-device-node (if available. Otherwise NULL) */ QFile *rndDev; #ifndef PWM_EMBEDDED /** mutex for accessing the public functions thread-save */ QMutex mutex; #endif /** seed value for fallback - rand_r() */ unsigned int seed; /** static Randomizer object returned by obj() */ static Randomizer *rndObj; }; #endif // __RANDOMIZER_H diff --git a/pwmanager/pwmanager/serializer.cpp b/pwmanager/pwmanager/serializer.cpp index 5c6568f..2d6498e 100644 --- a/pwmanager/pwmanager/serializer.cpp +++ b/pwmanager/pwmanager/serializer.cpp @@ -1,286 +1,286 @@ /*************************************************************************** * * * copyright (C) 2004 by Michael Buesch * * email: mbuesch@freenet.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License version 2 * * as published by the Free Software Foundation. * * * ***************************************************************************/ /*************************************************************************** * copyright (C) 2004 by Ulf Schenk - * This file is originaly based on version 2.0 of pwmanager + * This file is originaly based on version 1.1 of pwmanager * and was modified to run on embedded devices that run microkde * * $Id$ **************************************************************************/ #include "serializer.h" #include "pwmexception.h" #ifdef PWM_EMBEDDED #include <kglobal.h> #include <klocale.h> #endif /* enable/disable serializer debugging (0/1) */ -#define SERIALIZER_DEBUG 1 +#define SERIALIZER_DEBUG 0 /* use the old xml tags for writing (0/1) */ #define USE_OLD_TAGS 0 /* write a CDATA section (0/1) */ #define WRITE_CDATA_SEC 0 #define META_CREATE_DATE "c" #define META_VALID_DATE "v" #define META_EXPIRE_DATE "e" #define META_UPDATE_DATE "u" #define META_UPDATE_INT "i" //US ENH : uniqueid #define META_UNIQUEID "n" #define SYNC_ROOT "s" #define SYNC_TARGET_PREFIX "t" #define SYNC_TARGET_NAME "n" /* This is compatibility stuff. * The names of the entries have changed and here are the * new and old ones */ #define ROOT_MAGIC_OLD "PwM-xml-dat" #define VER_STR_OLD "ver" #define COMPAT_VER_OLD "0x02" #define CAT_ROOT_OLD "categories" #define CAT_PREFIX_OLD "cat_" #define CAT_NAME_OLD "name" #define ENTRY_PREFIX_OLD "entry_" #define ENTRY_DESC_OLD "desc" #define ENTRY_NAME_OLD "name" #define ENTRY_PW_OLD "pw" #define ENTRY_COMMENT_OLD "comment" #define ENTRY_URL_OLD "url" #define ENTRY_LAUNCHER_OLD "launcher" #define ENTRY_LVP_OLD "listViewPos" #define ENTRY_BIN_OLD "b" #define ENTRY_META_OLD "m" #define ROOT_MAGIC_NEW "P" #define VER_STR_NEW "v" #define COMPAT_VER_NEW "2" #define CAT_ROOT_NEW "c" #define CAT_PREFIX_NEW "c" #define CAT_NAME_NEW "n" #define ENTRY_PREFIX_NEW "e" #define ENTRY_DESC_NEW "d" #define ENTRY_NAME_NEW "n" #define ENTRY_PW_NEW "p" #define ENTRY_COMMENT_NEW "c" #define ENTRY_URL_NEW "u" #define ENTRY_LAUNCHER_NEW "l" #define ENTRY_LVP_NEW "v" #define ENTRY_BIN_NEW ENTRY_BIN_OLD #define ENTRY_META_NEW ENTRY_META_OLD #if USE_OLD_TAGS != 0 # define ROOT_MAGIC_WR ROOT_MAGIC_OLD # define VER_STR_WR VER_STR_OLD # define COMPAT_VER_WR COMPAT_VER_OLD # define CAT_ROOT_WR CAT_ROOT_OLD # define CAT_PREFIX_WR CAT_PREFIX_OLD # define CAT_NAME_WR CAT_NAME_OLD # define ENTRY_PREFIX_WR ENTRY_PREFIX_OLD # define ENTRY_DESC_WR ENTRY_DESC_OLD # define ENTRY_NAME_WR ENTRY_NAME_OLD # define ENTRY_PW_WR ENTRY_PW_OLD # define ENTRY_COMMENT_WR ENTRY_COMMENT_OLD # define ENTRY_URL_WR ENTRY_URL_OLD # define ENTRY_LAUNCHER_WR ENTRY_LAUNCHER_OLD # define ENTRY_LVP_WR ENTRY_LVP_OLD # define ENTRY_BIN_WR ENTRY_BIN_OLD # define ENTRY_META_WR ENTRY_META_OLD #else # define ROOT_MAGIC_WR ROOT_MAGIC_NEW # define VER_STR_WR VER_STR_NEW # define COMPAT_VER_WR COMPAT_VER_NEW # define CAT_ROOT_WR CAT_ROOT_NEW # define CAT_PREFIX_WR CAT_PREFIX_NEW # define CAT_NAME_WR CAT_NAME_NEW # define ENTRY_PREFIX_WR ENTRY_PREFIX_NEW # define ENTRY_DESC_WR ENTRY_DESC_NEW # define ENTRY_NAME_WR ENTRY_NAME_NEW # define ENTRY_PW_WR ENTRY_PW_NEW # define ENTRY_COMMENT_WR ENTRY_COMMENT_NEW # define ENTRY_URL_WR ENTRY_URL_NEW # define ENTRY_LAUNCHER_WR ENTRY_LAUNCHER_NEW # define ENTRY_LVP_WR ENTRY_LVP_NEW # define ENTRY_BIN_WR ENTRY_BIN_NEW # define ENTRY_META_WR ENTRY_META_NEW #endif Serializer::Serializer() { defaultLockStat = true; //US BUG: I needed to specify a document name. Otherwise impl will not be created for serializing #ifndef PWM_EMBEDDED domDoc = new QDomDocument; #else domDoc = new QDomDocument("mydoc"); #endif } Serializer::Serializer(const QCString &buffer) { defaultLockStat = true; //US BUG: I needed to specify a document name. Otherwise impl will not be created for serializing #ifndef PWM_EMBEDDED domDoc = new QDomDocument; #else domDoc = new QDomDocument("mydoc"); #endif if (!parseXml(buffer)) { delete domDoc; #ifndef PWM_EMBEDDED throw PwMException(PwMException::EX_PARSE); #else qDebug("Serializer::Serializer : Parse Exception "); #endif } } Serializer::~Serializer() { delete_ifnot_null(domDoc); } void Serializer::clear() { delete_ifnot_null(domDoc); domDoc = new QDomDocument; } bool Serializer::parseXml(const QCString &buffer) { PWM_ASSERT(domDoc); #ifndef PWM_EMBEDDED if (!domDoc->setContent(buffer, true)) return false; #else if (!domDoc->setContent(buffer)) return false; #endif if (!checkValid()) return false; return true; } QCString Serializer::getXml() { PWM_ASSERT(domDoc); #ifndef PWM_EMBEDDED #if defined(PWM_DEBUG) && SERIALIZER_DEBUG != 0 QCString tmp(domDoc->toCString(8)); printDebug("<BEGIN Serializer::getXml() dump>\n"); cout << tmp << endl; printDebug("<END Serializer::getXml() dump>"); #endif // DEBUG QCString ret(domDoc->toCString(0)); ret.replace('\n', ""); return ret; #else #if defined(PWM_DEBUG) && SERIALIZER_DEBUG != 0 QCString tmp(" " + domDoc->toCString()); printDebug("<BEGIN Serializer::getXml() dump>\n"); qDebug(tmp); cout << tmp << endl; printDebug("<END Serializer::getXml() dump>"); #endif // DEBUG QCString ret(domDoc->toCString()); ret.replace(QRegExp("\n"), ""); return ret; #endif } bool Serializer::serialize(PwMItem &dta) { PWM_ASSERT(domDoc); QDomElement root(genNewRoot()); QDomElement catNode(domDoc->createElement(CAT_ROOT_WR)); QDomElement syncNode(domDoc->createElement(SYNC_ROOT)); if (!addSyncData(&syncNode, dta.syncDta)) return false; root.appendChild(syncNode); if (!addCategories(&catNode, dta.dta)) return false; root.appendChild(catNode); return true; } bool Serializer::deSerialize(PwMItem *dta) { PWM_ASSERT(domDoc); PWM_ASSERT(dta); QDomElement root(domDoc->documentElement()); QDomNode n; dta->clear(); for (n = root.firstChild(); !n.isNull(); n = n.nextSibling()) { // find <categories> ... </categories> // <c> ... </c> if (n.nodeName() == CAT_ROOT_NEW || n.nodeName() == CAT_ROOT_OLD) { if (!readCategories(n, &(dta->dta))) { return false; } continue; } else if (n.nodeName() == SYNC_ROOT) { if (!readSyncData(n, &(dta->syncDta))) { return false; } continue; } /* NOTE: We can stop processing here, as we * don't have more nodes in root, yet. */ return false; } return true; } bool Serializer::readCategories(const QDomNode &n, vector<PwMCategoryItem> *dta) { QDomNodeList nl(n.childNodes()); QDomNode cur; QString name; unsigned int numCat = nl.count(), i; PwMCategoryItem curCat; vector<PwMDataItem> curEntr; if (!numCat) { printDebug("Serializer::readCategories(): empty"); return false; } for (i = 0; i < numCat; ++i) { cur = nl.item(i); if (cur.nodeName().left(1) == CAT_PREFIX_NEW || cur.nodeName().left(4) == CAT_PREFIX_OLD) { name = cur.toElement().attribute(CAT_NAME_NEW); if (name == QString::null) name = cur.toElement().attribute(CAT_NAME_OLD); PWM_ASSERT(name != QString::null); PWM_ASSERT(name != ""); curCat.clear(); curCat.name = name.latin1(); diff --git a/pwmanager/pwmanager/serializer.h b/pwmanager/pwmanager/serializer.h index ee61b94..df50e42 100644 --- a/pwmanager/pwmanager/serializer.h +++ b/pwmanager/pwmanager/serializer.h @@ -1,115 +1,115 @@ /*************************************************************************** * * * copyright (C) 2004 by Michael Buesch * * email: mbuesch@freenet.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License version 2 * * as published by the Free Software Foundation. * * * ***************************************************************************/ /*************************************************************************** * copyright (C) 2004 by Ulf Schenk - * This file is originaly based on version 2.0 of pwmanager + * This file is originaly based on version 1.1 of pwmanager * and was modified to run on embedded devices that run microkde * * $Id$ **************************************************************************/ #ifndef __SERIALIZER_H #define __SERIALIZER_H #include "pwmdoc.h" #include <qcstring.h> #include <qdom.h> #include <vector> using std::vector; /** This serializes its input data into * the PwManager-XML-datastream, that becomes * encrypted and maybe compressed */ class Serializer { public: /** construct an empty serializer document */ Serializer(); /** construct a serializer document and parse "buffer" */ Serializer(const QCString &buffer); /** destructor */ virtual ~Serializer(); /** clears all data */ void clear(); /** parse the given data buffer */ bool parseXml(const QCString &buffer); /** returns the current XML data */ QCString getXml(); /** serialize "dta" and store it as XML data */ //US ENH: we need to serialize and deserialize not only categories, but also synctargets bool serialize(PwMItem &dta); /** deserialize the (parsed) XML data and store it in "dta" */ bool deSerialize(PwMItem *dta); /** sets the initial default lockStat we should assign */ void setDefaultLockStat(bool stat) { defaultLockStat = stat; } protected: /** main data holder */ QDomDocument *domDoc; /** default lockStat to assign */ bool defaultLockStat; protected: /** check if this is valid PwManager XML data */ bool checkValid(); /** read the categories in the node "n" */ bool readCategories(const QDomNode &n, vector<PwMCategoryItem> *dta); /** read the entries in the node "n" */ bool readEntries(const QDomNode &n, vector<PwMDataItem> *dta); /** extract the data out of the given item at "n" */ bool extractEntry(const QDomNode &n, PwMDataItem *dta); /** extract the meta-data */ bool extractMeta(const QDomNode &n, PwMMetaData *dta); /** generates a new root node and sets all initial parameters */ QDomElement genNewRoot(); /** add new categories to the XML data stream in e */ bool addCategories(QDomElement *e, const vector<PwMCategoryItem> &dta); /** add the given new entries to the XML data stream in e */ bool addEntries(QDomElement *e, const vector<PwMDataItem> &dta); /** do serialize and write the given entry to the XML stream */ bool writeEntry(QDomElement *e, const PwMDataItem &_dta); /** write the entry meta data to the xml stream */ bool writeMeta(QDomElement *e, const PwMMetaData &dta); /** escape illegal characters out of the given entry data string */ QString escapeEntryData(QString dta); /** un-escape illegal characters out of the given entry data string */ QString unescapeEntryData(QString dta); //US ENH: the following methods are getting used to write/read sync entries /** read the syncentries in the node "n" */ bool readSyncData(const QDomNode &n, vector<PwMSyncItem> *dta); /** add new syncentries to the XML data stream in e */ bool addSyncData(QDomElement *e, const vector<PwMSyncItem> &dta); }; #endif // __SERIALIZER_H |