summaryrefslogtreecommitdiff
Side-by-side diff
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--noncore/apps/zsafe/zsafe.cpp2
1 files changed, 1 insertions, 1 deletions
diff --git a/noncore/apps/zsafe/zsafe.cpp b/noncore/apps/zsafe/zsafe.cpp
index 824b841..0864293 100644
--- a/noncore/apps/zsafe/zsafe.cpp
+++ b/noncore/apps/zsafe/zsafe.cpp
@@ -1164,1537 +1164,1537 @@ void ZSafe::showInfo( QListViewItem *_item)
if (!entry.isEmpty() && entry.compare(" "))
{
text += "<u><b>";
text += getFieldLabel (selectedItem, "3", tr("Password"));
text += ":<br> </b></u><blockquote>";
text += entry;
text += "</blockquote>";
// text += "<br>";
}
entry = selectedItem->text(4);
if (!entry.isEmpty() && entry.compare(" "))
{
text += "<u><b>";
text += getFieldLabel (selectedItem, "5", tr("Field 4"));
text += ":<br> </b></u><blockquote>";
text += entry;
text += "</blockquote>";
// text += "<br>";
}
entry = selectedItem->text(5);
if (!entry.isEmpty() && entry.compare(" "))
{
text += "<u><b>";
text += getFieldLabel (selectedItem, "6", tr("Field 5"));
text += ":<br> </b></u><blockquote>";
text += entry;
text += "</blockquote>";
// text += "<br>";
}
entry = selectedItem->text(3);
if (!entry.isEmpty() && entry.compare(" "))
{
text += "<u><b>";
text += getFieldLabel (selectedItem, "4", tr("Comment"));
text += ":<br> </b></u>";
QString comment = selectedItem->text(3);
comment.replace (QRegExp("\n"), "<br>");
text += comment;
// text += "<br>";
}
text += "</body></html>";
infoForm->InfoText->setText(text);
infoForm->hide();
QPEApplication::showDialog( infoForm );
}
}
void ZSafe::listViewSelected( QListViewItem *_item)
{
if (!_item)
return;
if (selectedItem != NULL)
selectedItem->setSelected(FALSE);
selectedItem = _item;
#ifndef DESKTOP
// set the column text dependent on the selected item
ListView->setColumnText(0, getFieldLabel (selectedItem, "1", tr("Name")));
ListView->setColumnText(1, getFieldLabel (selectedItem, "2", tr("Field 2")));
ListView->setColumnText(2, getFieldLabel (selectedItem, "3", tr("Field 3")));
ListView->setColumnText(3, getFieldLabel (selectedItem, "4", tr("Comment")));
ListView->setColumnText(4, getFieldLabel (selectedItem, "5", tr("Field 4")));
ListView->setColumnText(5, getFieldLabel (selectedItem, "6", tr("Field 5")));
#endif
#ifdef WIN32
// set the column text dependent on the selected item
ListView->setColumnText(0, getFieldLabel (selectedItem, "1", tr("Name")));
ListView->setColumnText(1, getFieldLabel (selectedItem, "2", tr("Field 2")));
ListView->setColumnText(2, getFieldLabel (selectedItem, "3", tr("Field 3")));
ListView->setColumnText(3, getFieldLabel (selectedItem, "4", tr("Comment")));
ListView->setColumnText(4, getFieldLabel (selectedItem, "5", tr("Field 4")));
ListView->setColumnText(5, getFieldLabel (selectedItem, "6", tr("Field 5")));
#endif
}
bool ZSafe::isCategory(QListViewItem *_item)
{
if (_item == NULL)
return FALSE;
QString categoryName = _item->text (0);
if (categories.find (categoryName))
return TRUE;
else
return FALSE;
}
void ZSafe::removeAsciiFile()
{
// QString fn = filename + ".txt";
// open the file dialog
#ifndef DESKTOP
#ifndef NO_OPIE
QMap<QString, QStringList> mimeTypes;
mimeTypes.insert(tr("All"), QStringList() );
mimeTypes.insert(tr("Text"), "text/*" );
QString fn = OFileDialog::getOpenFileName( OFileSelector::EXTENDED_ALL,
QDir::homeDirPath() + "/Documents/application/zsafe",
QString::null,
mimeTypes,
this,
tr ("Remove text file"));
#else
QString fn = ScQtFileEdit::getOpenFileName(this,
tr ("Remove text file"),
QDir::homeDirPath() + "/Documents/application/zsafe",
"*.txt");
#endif
#else
QString fn = QFileDialog::getOpenFileName(
QDir::homeDirPath() + "/Documents/application/zsafe",
"ZSafe (*.txt)",
this,
"ZSafe File Dialog"
"Choose a text file" );
#endif
if (fn && fn.length() > 0 )
{
QFile f( fn );
if ( !f.remove() )
{
#ifndef NO_OPIE
owarn << "Could not remove file " << fn << oendl;
#else
qWarning( QString("Could not remove file %1").arg(fn),2000 );
#endif
QMessageBox::critical( 0, tr("ZSafe"),
tr("Could not remove text file.") );
return;
}
}
}
void ZSafe::writeAllEntries()
{
if (filename.isEmpty())
{
QMessageBox::critical( 0, tr("ZSafe"),
tr("No document defined.\nYou have to create a new document"));
return;
}
// open the file dialog
#ifndef DESKTOP
#ifndef NO_OPIE
QMap<QString, QStringList> mimeTypes;
mimeTypes.insert(tr("All"), QStringList() );
mimeTypes.insert(tr("Text"), "text/*" );
QString fn = OFileDialog::getSaveFileName( OFileSelector::EXTENDED_ALL,
QDir::homeDirPath() + "/Documents/application/zsafe",
QString::null,
mimeTypes,
this,
tr ("Export text file"));
#else
QString fn = ScQtFileEdit::getSaveAsFileName(this,
tr ("Export text file"),
QDir::homeDirPath() + "/Documents/application/zsafe",
"*.txt");
#endif
#else
QString fn = QFileDialog::getSaveFileName(
QDir::homeDirPath() + "/Documents/application/zsafe",
"ZSafe (*.txt)",
this,
"ZSafe File Dialog"
"Choose a text file" );
#endif
// open the new document
if (fn && fn.length() > 0 )
{
QFile f( fn );
if ( !f.open( IO_WriteOnly ) ) {
#ifndef NO_OPIE
owarn << "Could not write to file " << fn << oendl;
#else
qWarning( QString("Could not write to file %1").arg(fn),2000 );
#endif
QMessageBox::critical( 0, "ZSafe",
QString("Could not export to text file.") );
return;
}
QTextStream t( &f );
QListViewItem *i;
// step through all categories
for (i = ListView->firstChild();
i != NULL;
i = i->nextSibling())
{
// step through all subitems
QListViewItem *si;
for (si = i->firstChild();
si != NULL;
si = si->nextSibling())
{
QString oneEntry;
oneEntry += "\"";
oneEntry += i->text(0);
oneEntry += "\";";
oneEntry += "\"";
oneEntry += si->text(0);
oneEntry += "\";";
oneEntry += "\"";
oneEntry += si->text(1);
oneEntry += "\";";
oneEntry += "\"";
oneEntry += si->text(2);
oneEntry += "\";";
oneEntry += "\"";
QString comment = si->text(3);
comment.replace (QRegExp("\n"), "<br>");
oneEntry += comment;
oneEntry += "\";";
oneEntry += "\"";
oneEntry += si->text(4);
oneEntry += "\";";
oneEntry += "\"";
oneEntry += si->text(5);
oneEntry += "\"";
// owarn << oneEntry << oendl;
t << oneEntry << endl;
// owarn << si->text(0) << oendl;
}
}
f.close();
}
}
void ZSafe::readAllEntries()
{
if (filename.isEmpty())
{
QMessageBox::critical( 0, tr("ZSafe"),
tr("No document defined.\nYou have to create a new document"));
return;
}
// open the file dialog
#ifndef DESKTOP
#ifndef NO_OPIE
QMap<QString, QStringList> mimeTypes;
mimeTypes.insert(tr("All"), QStringList() );
mimeTypes.insert(tr("Text"), "text/*" );
QString fn = OFileDialog::getOpenFileName( OFileSelector::EXTENDED_ALL,
QDir::homeDirPath() + "/Documents/application/zsafe",
QString::null,
mimeTypes,
this,
tr ("Import text file"));
#else
QString fn = ScQtFileEdit::getOpenFileName(this,
tr ("Import text file"),
QDir::homeDirPath() + "/Documents/application/zsafe",
"*.txt");
#endif
#else
QString fn = QFileDialog::getOpenFileName(
QDir::homeDirPath() + "/Documents/application/zsafe",
"ZSafe (*.txt)",
this,
"ZSafe File Dialog"
"Choose a text file" );
#endif
if (fn && fn.length() > 0 )
{
QFile f( fn );
if ( !f.open( IO_ReadOnly ) )
{
#ifndef NO_OPIE
owarn << "Could not read file " << fn << oendl;
#else
qWarning( QString("Could not read file %1").arg(fn), 2000 );
#endif
QMessageBox::critical( 0, "ZSafe",
QString("Could not import text file.") );
return;
}
modified = true;
// clear the password list
selectedItem = NULL;
QListViewItem *i;
// step through all categories
for (i = ListView->firstChild();
i != NULL;
i = i->nextSibling())
{
// step through all subitems
QListViewItem *si;
for (si = i->firstChild();
si != NULL; )
// si = si->nextSibling())
{
QListViewItem *_si = si;
si = si->nextSibling();
i->takeItem(_si); // remove from view list
if (_si) delete _si;
}
}
#ifndef NO_OPIE
owarn << "ReadAllEntries(): " << oendl;
#else
qWarning ("ReadAllEntries(): ");
#endif
QTextStream t(&f);
while ( !t.eof() )
{
QString s = t.readLine();
s.replace (QRegExp("\";\""), "\"|\"");
// char buffer[1024];
#ifndef WIN32
char buffer[s.length()+1];
#else
char buffer[4048];
#endif
/* modify QString -> QCString::utf8 */
strcpy (buffer, s.utf8());
QString name;
QString user;
QString password;
QString comment;
QString field5="";
QString field6="";
// separete the entries
char *i = strtok (buffer, "|");
QString category(QString::fromUtf8(&i[1]));
category.truncate(category.length() -1);
int idx=0;
while ((i = strtok (NULL, "|")) != NULL)
{
switch (idx)
{
case 0:
name = QString::fromUtf8(&i[1]);
name.truncate(name.length() -1);
// name
break;
case 1:
// user
user = QString::fromUtf8(&i[1]);
user.truncate(user.length() -1);
break;
case 2:
// password
password = QString::fromUtf8(&i[1]);
password.truncate(password.length() -1);
break;
case 3:
// comment
comment = QString::fromUtf8(&i[1]);
comment.truncate(comment.length() -1);
break;
case 4:
// field5
field5 = QString::fromUtf8(&i[1]);
field5.truncate(field5.length() -1);
break;
case 5:
// field6
field6 = QString::fromUtf8(&i[1]);
field6.truncate(field6.length() -1);
break;
}
idx++;
}
Category *cat= categories.find (category);
if (cat)
{
// use the existend item
QListViewItem *catItem = cat->getListItem();
if (catItem)
{
QListViewItem * item = new ShadedListItem( 0, catItem );
item->setText( 0, tr( name ) );
item->setText( 1, tr( user ) );
item->setText( 2, tr( password ) );
item->setText( 3, tr( comment ) );
item->setText( 4, tr( field5 ) );
item->setText( 5, tr( field6 ) );
catItem->setOpen( TRUE );
}
}
else
{
QListViewItem *catI = new ShadedListItem( 1, ListView );
// create and insert a new item
QListViewItem * item = new ShadedListItem( 0, catI );
item->setText( 0, tr( name ) );
item->setText( 1, tr( user ) );
item->setText( 2, tr( password ) );
item->setText( 3, tr( comment ) );
item->setText( 4, tr( field5 ) );
item->setText( 5, tr( field6 ) );
catI->setOpen( TRUE );
Category *c1 = new Category();
c1->setCategoryName(category);
QString icon;
QString fullIconPath;
QPixmap *pix;
// #ifndef WIN32
icon = conf->readEntry(APP_KEY+category);
// #endif
bool isIconAv = false;
if (!icon.isEmpty() && !icon.isNull())
{
// build the full path
fullIconPath = iconPath + icon;
pix = new QPixmap (fullIconPath);
if (!pix->isNull())
{
QImage img = pix->convertToImage();
pix->convertFromImage(img.smoothScale(14,14));
c1->setIconName (icon);
c1->setIcon (*pix);
isIconAv = true;
}
}
if (!isIconAv)
{
c1->setIcon (*getPredefinedIcon(category));
}
c1->setListItem (catI);
c1->initListItem();
categories.insert (c1->getCategoryName(), c1);
}
}
f.close();
}
else
{
}
}
#ifdef UNUSED
void ZSafe::writeAllEntries()
{
if (filename.isEmpty())
{
QMessageBox::critical( 0, tr("ZSafe"),
tr("<P>No document defined. You have to create a new document</P>"));
return;
}
// open the file for writing
QString fn = filename + ".txt";
QFile f( fn );
if ( !f.open( IO_WriteOnly ) ) {
#ifndef NO_OPIE
owarn << "Could not write to file " << fn << oendl;
#else
qWarning( QString("Could not write to file %1").arg(fn), 2000 );
#endif
QMessageBox::critical( 0, tr("ZSafe"),
tr("Could not export to text file.") );
return;
}
QTextStream t( &f );
QListViewItem *i;
// step through all categories
for (i = ListView->firstChild();
i != NULL;
i = i->nextSibling())
{
// step through all subitems
QListViewItem *si;
for (si = i->firstChild();
si != NULL;
si = si->nextSibling())
{
QString oneEntry;
oneEntry += "\"";
oneEntry += i->text(0);
oneEntry += "\";";
oneEntry += "\"";
oneEntry += si->text(0);
oneEntry += "\";";
oneEntry += "\"";
oneEntry += si->text(1);
oneEntry += "\";";
oneEntry += "\"";
oneEntry += si->text(2);
oneEntry += "\";";
oneEntry += "\"";
// oneEntry += si->text(3);
QString comment = si->text(3);
comment.replace (QRegExp("\n"), "<br>");
oneEntry += comment;
oneEntry += "\"";
// owarn << oneEntry << oendl;
t << oneEntry << endl;
// owarn << si->text(0) << oendl;
}
}
f.close();
}
void ZSafe::readAllEntries()
{
QString fn = filename + ".txt";
QFile f( fn );
if ( !f.open( IO_ReadOnly ) )
{
#ifndef NO_OPIE
owarn << "Could not read file " << fn << oendl;
#else
qWarning( QString("Could not read file %1").arg(fn), 2000 );
#endif
QMessageBox::critical( 0, tr("ZSafe"),
tr("Could not import text file.") );
return;
}
modified = true;
// clear the password list
selectedItem = NULL;
QListViewItem *i;
// step through all categories
for (i = ListView->firstChild();
i != NULL;
i = i->nextSibling())
{
// step through all subitems
QListViewItem *si;
for (si = i->firstChild();
si != NULL; )
// si = si->nextSibling())
{
QListViewItem *_si = si;
si = si->nextSibling();
i->takeItem(_si); // remove from view list
if (_si) delete _si;
}
}
#ifndef NO_OPIE
owarn << "ReadAllEntries(): " << oendl;
#else
qWarning ("ReadAllEntries(): ");
#endif
QTextStream t(&f);
while ( !t.eof() )
{
QString s = t.readLine();
s.replace (QRegExp("\";\""), "\"|\"");
// char buffer[1024];
int len=s.length()+1;
#ifdef WIN32
char buffer[512];
#else
char buffer[len];
#endif
strcpy (buffer, s);
QString name;
QString user;
QString password;
QString comment;
// separete the entries
char *i = strtok (buffer, "|");
QString category(&i[1]);
category.truncate(category.length() -1);
int idx=0;
while (i = strtok (NULL, "|"))
{
switch (idx)
{
case 0:
name = &i[1];
name.truncate(name.length() -1);
// name
break;
case 1:
// user
user = &i[1];
user.truncate(user.length() -1);
break;
case 2:
// password
password = &i[1];
password.truncate(password.length() -1);
break;
case 3:
// comment
comment = &i[1];
comment.truncate(comment.length() -1);
break;
}
idx++;
}
Category *cat= categories.find (category);
if (cat)
{
// use the existend item
QListViewItem *catItem = cat->getListItem();
if (catItem)
{
QListViewItem * item = new ShadedListItem( 0, catItem );
item->setText( 0, tr( name ) );
item->setText( 1, tr( user ) );
item->setText( 2, tr( password ) );
item->setText( 3, tr( comment ) );
catItem->setOpen( TRUE );
}
}
else
{
QListViewItem *catI = new ShadedListItem( 1, ListView );
// create and insert a new item
QListViewItem * item = new ShadedListItem( 0, catI );
item->setText( 0, tr( name ) );
item->setText( 1, tr( user ) );
item->setText( 2, tr( password ) );
item->setText( 3, tr( comment ) );
catI->setOpen( TRUE );
Category *c1 = new Category();
c1->setCategoryName(category);
QString icon;
QString fullIconPath;
QPixmap *pix;
// #ifndef WIN32
icon = conf->readEntry(APP_KEY+category);
// #endif
bool isIconAv = false;
if (!icon.isEmpty() && !icon.isNull())
{
// build the full path
fullIconPath = iconPath + icon;
pix = new QPixmap (fullIconPath);
if (!pix->isNull())
{
QImage img = pix->convertToImage();
pix->convertFromImage(img.smoothScale(14,14));
c1->setIconName (icon);
c1->setIcon (*pix);
isIconAv = true;
}
}
if (!isIconAv)
{
c1->setIcon (*getPredefinedIcon(category));
}
c1->setListItem (catI);
c1->initListItem();
categories.insert (c1->getCategoryName(), c1);
}
}
f.close();
}
#endif // UNUSED
void ZSafe::resume(int)
{
#ifndef NO_OPIE
owarn << "Resume" << oendl;
#endif
// hide the main window
if ( !showpwd )
{
infoForm->hide();
// open zsafe again
m_password = "";
selectedItem = NULL;
// clear the password list
QListViewItem *i;
// step through all categories
for (i = ListView->firstChild();
i != NULL;
i = i->nextSibling())
{
// step through all subitems
QListViewItem *si;
for (si = i->firstChild();
si != NULL; )
{
QListViewItem *_si = si;
si = si->nextSibling();
i->takeItem(_si); // remove from view list
if (_si) delete _si;
}
}
// ask for password and read again
openDocument(filename);
}
}
//---------------------------------------------
bool ZSafe::openDocument(const char* _filename, const char* )
{
int retval;
char* entry[FIELD_SIZE];
// #ifndef WIN32
int validationFlag = conf->readNumEntry(APP_KEY+"valzsafe", 1);
// #else
// int validationFlag = 1;
// #endif
int pwdOk = 0;
int numberOfTries = 3;
for (int i=0; i < numberOfTries; i++)
{
QFile f(_filename);
if (f.exists())
{
// ask with a dialog for the password
if (m_password.isEmpty())
getDocPassword(tr("Enter Password"));
if (m_password.isEmpty() && validationFlag == 0)
{
#ifndef NO_OPIE
owarn << "Wrong password" << oendl;
#else
qWarning ("Wrong password");
#endif
QMessageBox::critical( 0, tr("ZSafe"),
tr("Wrong password.\n\nZSafe will now exit.") );
exitZs (1);
}
retval = loadInit(_filename, m_password);
if (retval != PWERR_GOOD)
{
#ifndef NO_OPIE
owarn << "Error loading Document" << oendl;
-#lese
+#else
qWarning ("Error loading Document");
#endif
return false;
}
}
else
{
#ifdef WIN32
this->setCaption("Qt ZSafe");
#else
this->setCaption("ZSafe");
#endif
filename = "";
return false;
}
// load the validation entry
if (validationFlag == 0)
{
pwdOk = 1;
break;
}
retval = loadEntry(entry);
if (retval == 1 &&
!strcmp (entry[0], "ZSAFECATEGORY") &&
!strcmp (entry[1], "name") &&
!strcmp (entry[2], "username") &&
!strcmp (entry[3], "password") &&
!strcmp (entry[4], "comment") )
{
for (int count = 0; count < FIELD_SIZE; count++) free(entry[count]);
pwdOk = 1;
break;
}
else
// for (int count = 0; count < FIELD_SIZE; count++) free(entry[count]);
fclose (fd);
m_password = "";
if (i < numberOfTries - 1)
{
switch( QMessageBox::warning( this, tr("ZSafe"),
tr("Wrong password.\nEnter again?"),
tr("&Yes"), tr("&No."),
0
) )
{
case 1: // No
exitZs (1);
break;
case 0: // Yes
continue;
}
}
}
if (pwdOk == 0)
{
// unset the document entry
conf->writeEntry(APP_KEY+"document", "INVALIDPWD");
if (conf)
delete conf;
exitZs (1);
}
retval = loadEntry(entry);
int numberOfEntries=0;
while (retval == 1) {
QString category( QString::fromUtf8(entry[0]) );
QString name( QString::fromUtf8(entry[1]) );
QString user( QString::fromUtf8(entry[2]) );
QString password( QString::fromUtf8(entry[3]) );
QString comment( QString::fromUtf8(entry[4]) );
QString field5( QString::fromUtf8(entry[5]) );
QString field6( QString::fromUtf8(entry[6]) );
// add the subitems to the categories
Category *cat= categories.find (category);
if (cat)
{
// use the existend item
QListViewItem *catItem = cat->getListItem();
if (catItem)
{
QListViewItem * item = new ShadedListItem( 0, catItem );
item->setText( 0, tr( name ) );
item->setText( 1, tr( user ) );
item->setText( 2, tr( password ) );
item->setText( 3, tr( comment ) );
item->setText( 4, tr( field5 ) );
item->setText( 5, tr( field6 ) );
if (expandTree)
catItem->setOpen( TRUE );
numberOfEntries++;
}
}
else
{
QListViewItem *catI = new ShadedListItem( 1, ListView );
// create and insert a new item
QListViewItem * item = new ShadedListItem( 0, catI );
item->setText( 0, tr( name ) );
item->setText( 1, tr( user ) );
item->setText( 2, tr( password ) );
item->setText( 3, tr( comment ) );
item->setText( 4, tr( field5 ) );
item->setText( 5, tr( field6 ) );
if (expandTree)
catI->setOpen( TRUE );
Category *c1 = new Category();
c1->setCategoryName(category);
QString icon;
QString fullIconPath;
QPixmap *pix;
// #ifndef WIN32
icon = conf->readEntry(APP_KEY+category);
// #endif
bool isIconAv = false;
if (!icon.isEmpty() && !icon.isNull())
{
// build the full path
fullIconPath = iconPath + icon;
pix = new QPixmap (fullIconPath);
if (!pix->isNull())
{
QImage img = pix->convertToImage();
pix->convertFromImage(img.smoothScale(14,14));
c1->setIconName (icon);
c1->setIcon (*pix);
isIconAv = true;
}
}
if (!isIconAv)
{
c1->setIcon (*getPredefinedIcon(category));
}
c1->setListItem (catI);
c1->initListItem();
categories.insert (c1->getCategoryName(), c1);
numberOfEntries++;
}
for (int count = 0; count < FIELD_SIZE; count++) {
free(entry[count]);
}
retval = loadEntry(entry);
if (retval == 2) {
// m_parent->slotStatusHelpMsg("Last entry loaded");
}
} // end while
if (numberOfEntries == 0)
{
switch( QMessageBox::warning( this, tr("ZSafe"),
tr("Empty document or\nwrong password.\nContinue?"),
tr("&No"), tr("&Yes."),
0
) ) {
case 0: // No
retval = loadFinalize();
exitZs (1);
break;
case 1: // Yes
break;
}
}
retval = loadFinalize();
return true;
}
int ZSafe::loadInit(const char* _filename, const char *password)
{
unsigned int j = 0;
unsigned int keylength=0;
int count=0, count2=0, count3=0;
unsigned char charbuf[8];
unsigned short ciphertext[4];
char key[128];
Krc2* krc2 = new Krc2();
fd = fopen (_filename, "rb");
QFileInfo f (_filename);
load_buffer_length = f.size();
load_buffer_length = ((load_buffer_length / 1024)+1) * 1024 * 2;
if (fd == NULL)
return PWERR_OPEN;
buffer = (char *)malloc(load_buffer_length);
for (j = 0; password[j] != '\0'; j++) {
key[j] = password[j];
}
keylength = j;
krc2->rc2_expandkey (key, keylength, 128);
#ifndef WIN32
size = read(fileno (fd), (unsigned char *) (charbuf + count), 8);
#else
size = fread ((unsigned char *) (charbuf + count), sizeof(unsigned char), 8, fd);
#endif
if (size < 8)
return PWERR_DATA;
for (count = 0; count < 4; count++) {
count2 = count << 1;
iv[count] = charbuf[count2] << 8;
iv[count] += charbuf[count2 + 1];
}
size = 0;
bufferIndex = 0;
#ifndef WIN32
while ((count = read (fileno (fd), (unsigned char *) charbuf, 8)) > 0) {
while (count < 8) {
count2 = read (fileno (fd), (unsigned char *) (charbuf + count), 8);
#else
while ((count = fread ((unsigned char *) (charbuf), sizeof(unsigned char), 8, fd)) > 0) {
while (count < 8) {
count2 = fread ((unsigned char *) (charbuf + count), sizeof(unsigned char), 8, fd);
#endif
if (count2 == 0) {
return PWERR_DATA;
}
count += count2;
} /* while (count < 8) */
size += 8;
for (count2 = 0; count2 < 8; count2 += 2) {
count3 = count2 >> 1;
ciphertext[count3] = charbuf[count2] << 8;
ciphertext[count3] += charbuf[count2 + 1];
plaintext[count3] = ciphertext[count3] ^ iv[count3];
iv[count3] = plaintext[count3];
} /* for (count2) */
krc2->rc2_decrypt (plaintext);
memcpy ((unsigned char *) (buffer + bufferIndex), plaintext, 8);
bufferIndex += 8;
buffer[bufferIndex + 1] = '\0';
} /* while ((count = read (fileno (fd), (unsigned char *) charbuf, 8)) > 0) */
size -= buffer[size - 1];
lastcount = 0;
/* This will point to the starting index */
bufferIndex = 0;
return PWERR_GOOD;
}
int ZSafe::loadEntry(char *entry[FIELD_SIZE])
{
/* Strip off PKCS 5 padding
* Should check to make sure it's good here
*/
int count, count1=0;
for (count = lastcount; count < size; count++) {
if ((unsigned char) (buffer[count]) == 255) {
if (buffer[bufferIndex] == '\0') {
bufferIndex++;
}
entry[count1] = (char *) malloc (count - bufferIndex + 1);
memcpy (entry[count1], (unsigned char *) (buffer + bufferIndex), count - bufferIndex);
entry[count1][count - bufferIndex] = '\0';
count++;
bufferIndex = count;
count1++;
if (count1 == FIELD_SIZE) {
lastcount = count;
return 1;
}
} /* if ((unsigned char) (buffer[count]) == 255) */
} /* for (count = 0; count < size; count++) */
return 2;
}
int ZSafe::loadFinalize(void)
{
fclose (fd);
if (buffer) free(buffer);
return PWERR_GOOD;
}
bool ZSafe::saveDocument(const char* _filename,
bool withPwd,
const char* )
{
if (filename.isEmpty())
{
QMessageBox::critical( 0, tr("ZSafe"),
tr("No document defined.\nYou have to create a new document"));
return false;
}
// if (m_password.isEmpty())
// withPwd = true; // the document must be saved with a valid password
if (withPwd)
{
bool pwdOk = FALSE;
while (!pwdOk)
{
getDocPassword(tr("Enter Password"));
if (m_password.isEmpty())
{
QMessageBox::critical( 0, tr("ZSafe"),
tr("Password is empty.\nPlease enter again."));
continue;
}
QString firstPasswd = m_password;
getDocPassword(tr("Reenter Password"));
if (m_password.isEmpty())
{
QMessageBox::critical( 0, tr("ZSafe"),
tr("Password is empty.\nPlease enter again."));
continue;
}
if (firstPasswd != m_password)
{
QMessageBox::critical( 0, tr("ZSafe"),
tr("Passwords must be identical.\nPlease enter again."));
continue;
}
pwdOk = TRUE;
modified = false;
}
}
else if (modified)
{
QString fns(_filename);
fns = fns.right (fns.length() - fns.findRev ('/') - 1);
switch( QMessageBox::information( this, tr("ZSafe"),
tr("Do you want to save ") + fns + tr("\nbefore continuing?"),
tr("&Save"),
tr("&Don't Save"),
0 // Enter == button 0
) )
{ // Escape == button 2
case 0: // Save clicked, Alt-S or Enter pressed.
modified = false;
break;
case 1: // Don't Save clicked or Alt-D pressed
modified = false;
return true;
}
}
modified = false;
if (m_password.isEmpty())
return false;
int retval = saveInit(_filename, m_password);
// int retval = saveInit(_filename, "test");
if (retval != PWERR_GOOD) {
return false;
}
char* entry[FIELD_SIZE];
// save the validation entry
{
int i=0;
entry[i] = (char*)malloc(strlen("ZSAFECATEGORY")+1);
strcpy(entry[i++], "ZSAFECATEGORY");
entry[i] = (char*)malloc(strlen("name")+1);
strcpy(entry[i++], "name");
entry[i] = (char*)malloc(strlen("username")+1);
strcpy(entry[i++], "username");
entry[i] = (char*)malloc(strlen("password")+1);
strcpy(entry[i++], "password");
entry[i] = (char*)malloc(strlen("comment")+1);
strcpy(entry[i++], "comment");
entry[i] = (char*)malloc(strlen("field5")+1);
strcpy(entry[i++], "field5");
entry[i] = (char*)malloc(strlen("field6")+1);
strcpy(entry[i++], "field6");
retval = saveEntry(entry);
for (int z=0; z<i; z++) free(entry[z]);
if (retval == PWERR_DATA) {
#ifndef NO_OPIE
owarn << "1: Error writing file, contents not saved" << oendl;
#else
qWarning("1: Error writing file, contents not saved");
#endif
saveFinalize();
return false;
}
// #ifndef WIN32
conf->writeEntry(APP_KEY+"valzsafe", 1);
// #endif
saveConf();
}
QListViewItem *i;
// step through all categories
for (i = ListView->firstChild();
i != NULL;
i = i->nextSibling())
{
// step through all subitems
QListViewItem *si;
for (si = i->firstChild();
si != NULL;
si = si->nextSibling())
{
int j=0;
entry[j] = (char*)malloc(strlen(i->text(0).utf8())+1);
strcpy(entry[j++], i->text(0).utf8());
entry[j] = (char*)malloc(strlen(si->text(0).utf8())+1);
strcpy(entry[j++], si->text(0).utf8());
entry[j] = (char*)malloc(strlen(si->text(1).utf8())+1);
strcpy(entry[j++], si->text(1).utf8());
entry[j] = (char*)malloc(strlen(si->text(2).utf8())+1);
strcpy(entry[j++], si->text(2).utf8());
entry[j] = (char*)malloc(strlen(si->text(3).utf8())+1);
strcpy(entry[j++], si->text(3).utf8());
entry[j] = (char*)malloc(strlen(si->text(4).utf8())+1);
strcpy(entry[j++], si->text(4).utf8());
entry[j] = (char*)malloc(strlen(si->text(5).utf8())+1);
strcpy(entry[j++], si->text(5).utf8());
retval = saveEntry(entry);
for (int z=0; z<j; z++)
{
free(entry[z]);
}
if (retval == PWERR_DATA) {
#ifndef NO_OPIE
owarn << "1: Error writing file, contents not saved" << oendl;
#else
qWarning("1: Error writing file, contents not saved");
#endif
saveFinalize();
return false;
}
}
}
if (saveFinalize() == PWERR_DATA) {
#ifndef NO_OPIE
owarn << "2: Error writing file, contents not saved" << oendl;
#else
qWarning("2: Error writing file, contents not saved");
#endif
return false;
} else {
#ifndef DESKTOP
Global::statusMessage (tr("Password file saved."));
#endif
modified = false;
return true;
}
}
PasswordForm *newPwdDialog;
bool newPwdDialogResult = false;
void ZSafe::setPasswordDialogDone()
{
newPwdDialogResult = true;
newPwdDialog->close();
}
void ZSafe::getDocPassword(QString title)
{
#ifndef NO_OPIE
owarn << "getDocPassword" << oendl;
#endif
// open the 'Password' dialog
PasswordForm *dialog = new PasswordForm(this, title, TRUE);
newPwdDialog = dialog;
newPwdDialogResult = false;
QPixmap image0( ( const char** ) zsafe_xpm );
dialog->setIcon( image0);
connect( dialog->PasswordField, SIGNAL( returnPressed() ),
this, SLOT( setPasswordDialogDone() ) );
// CS: !!!
// int pos = filename.findRev ('/');
QString ti = filename.right (filename.length() - filename.findRev ('/') - 1);
#ifdef WIN32
dialog->setCaption("Qt " + ti);
#else
dialog->setCaption(ti);
#endif
// dialog->setCaption(title);
dialog->PasswordField->setFocus();
DialogCode result = (DialogCode) dialog->exec();
#ifdef DESKTOP
result = Accepted;
#endif
QString password;
if (result == Accepted || newPwdDialogResult)
{
m_password = dialog->PasswordField->text();
}
else
{
exitZs (1);
}
}
int ZSafe::saveInit(const char *_filename, const char *password)
{
char key[128];
unsigned int j = 0;
unsigned int keylength;
// int val;
int count2;
Krc2* krc2 = new Krc2();
/* first we should check the permissions of the filename */
/*
if (QFile::exists(_filename)) {
val = checkFile(_filename);
if (val != PWERR_GOOD)
return val;
} else
{
val = creat (_filename, (S_IRUSR | S_IWUSR));
if (val == -1)
return PWERR_OPEN;
else
close(val);
}
*/
QFileInfo f (_filename);
save_buffer_length = f.size();
save_buffer_length = ((save_buffer_length / 1024)+1) * 1024;
fd = fopen (_filename, "wb");
if (fd == NULL)
return PWERR_OPEN;
buffer = (char*)malloc(save_buffer_length);
/* make the key ready */
for (j = 0; password[j] != '\0'; j++) {
key[j] = password[j];
}
keylength = j;
krc2->rc2_expandkey (key, keylength, 128);
/* First, we make the IV */
for (count2 = 0; count2 < 4; count2++) {
iv[count2] = rand ();
putc ((unsigned char) (iv[count2] >> 8), fd);
putc ((unsigned char) (iv[count2] & 0xff), fd);
}
bufferIndex = 0;
return PWERR_GOOD;
}
int ZSafe::saveEntry(char *entry[FIELD_SIZE])
{
char *text1;
int count2, count3;
unsigned short ciphertext[4];
Krc2* krc2 = new Krc2();
buffer = (char*)memset(buffer, '\0', save_buffer_length);
for (count2 = 0; count2 < FIELD_SIZE; count2++) {
text1 = entry[count2];
if (strlen (text1) == 0) {
strncat(buffer, " ", strlen(" "));
} else {
strncat(buffer, text1, strlen(text1));
}
/* Use 255 as the marker. \n is too tough to test for */
buffer[strlen (buffer)] = 255;
} /*for (count2 = 0; count2 < 5; count2++)*/
count2 = 0;
/* I'm using CBC mode and encrypting the data straight from top down.
* At the bottom, encrypted, I will append an MD5 hash of the file, eventually.
* PKCS 5 padding (explained at the code section
*/
while (count2 < (int)strlen (buffer)) {
#ifndef WORDS_BIGENDIAN
plaintext[bufferIndex] = buffer[count2 + 1] << 8;
plaintext[bufferIndex] += buffer[count2] & 0xff;
#endif
#ifdef WORDS_BIGENDIAN
plaintext[bufferIndex] = buffer[count2] << 8;
plaintext[bufferIndex] += buffer[count2 + 1] & 0xff;
#endif
bufferIndex++;
if (bufferIndex == 4) {
krc2->rc2_encrypt (plaintext);
for (count3 = 0; count3 < 4; count3++) {
ciphertext[count3] = iv[count3] ^ plaintext[count3];
/* Now store the ciphertext as the iv */
iv[count3] = plaintext[count3];
/* reset the buffer index */
bufferIndex = 0;
if (putc ((unsigned char) (ciphertext[count3] >> 8), fd) == EOF) return PWERR_DATA;
if (putc ((unsigned char) (ciphertext[count3] & 0xff), fd) == EOF) return PWERR_DATA;
} /*for (count3 = 0; count3 < 5; count3++)*/
} /*if (bufferIndex == 5)*/
/* increment a short, not a byte */
count2 += 2;
} /*while (count2 < strlen (buffer))*/
int ret = PWERR_GOOD;
return ret;
}
int ZSafe::saveFinalize(void)
{
int count1, retval = PWERR_GOOD;
unsigned short ciphertext[4];
Krc2* krc2 = new Krc2();
/* Tack on the PKCS 5 padding
* How it works is we fill up the last n bytes with the value n
*
* So, if we have, say, 13 bytes, 8 of which are used, we have 5 left
* over, leaving us 3 short, so we fill it in with 3's.
*
* If we come out even, we fill it with 8 8s
*
* um, except that in this instance we are using 4 shorts instead of 8 bytes.
* so, half everything
*/
for (count1 = bufferIndex; count1 < 4; count1++) {
plaintext[count1] = (4 - bufferIndex);
}
krc2->rc2_encrypt (plaintext);
for (count1 = 0; count1 < 4; count1++) {
ciphertext[count1] = iv[count1] ^ plaintext[count1];
if (putc ((unsigned char) (ciphertext[count1] >> 8), fd) == EOF) retval = PWERR_DATA;
if (putc ((unsigned char) (ciphertext[count1] & 0xff), fd) == EOF) retval = PWERR_DATA;
}
fclose (fd);
free(buffer);
return retval;
}
void ZSafe::quitMe ()
{
#ifndef NO_OPIE
owarn << "QUIT..." << oendl;
#endif
if (modified)
{
switch( QMessageBox::information( this, tr("ZSafe"),
tr("Do you want to save\nbefore exiting?"),
tr("&Save"),
tr("S&ave with\nnew\npassword"),
tr("&Don't Save"),
0 // Enter == button 0
) )
{ // Escape == button 2
case 0: // Save clicked, Alt-S or Enter pressed.
// save
modified = false;
saveDocument(filename, FALSE);
exitZs (1);
break;
case 1: //
// Save with new password
modified = false;
saveDocument(filename, TRUE);
exitZs (1);
break;
case 2: // Don't Save clicked or Alt-D pressed
// don't save but exitZs
exitZs (1);
break;
}
}
exitZs (1);
}
void ZSafe::categoryFieldActivated( const QString& category)
{
if (categoryDialog)
setCategoryDialogFields(categoryDialog, category);
}
void ZSafe::addCategory()
{
if (filename.isEmpty())
{
QMessageBox::critical( 0, tr("ZSafe"),
tr("No document defined.\nYou have to create a new document"));
return;
}
else
{
// open the 'Category' dialog
bool initIcons = false;
// open the 'Category' dialog
CategoryDialog *dialog;
if (categoryDialog)
{
dialog = categoryDialog;
}
else
{
categoryDialog = new CategoryDialog(this, tr("Category"), TRUE);
#ifdef WIN32
categoryDialog->setCaption ("Qt " + tr("Category"));
#endif
dialog = categoryDialog;
connect( dialog->CategoryField,
SIGNAL( activated(const QString&)),
this, SLOT( categoryFieldActivated(const QString&) ) );
initIcons = true;
}
#ifdef DESKTOP
#ifndef WIN32
QStringList list = conf->entryList( APP_KEY+"/fieldDefs" );
#else
// read all categories from the config file and store
// into a list
QFile f (cfgFile);
QStringList list;
if ( f.open(IO_ReadOnly) ) { // file opened successfully
QTextStream t( &f ); // use a text stream
QString s;
int n = 1;
while ( !t.eof() ) { // until end of file...
s = t.readLine(); // line of text excluding '\n'
list.append(s);
}
f.close();
}
#endif
#else
// read all categories from the config file and store
// into a list
QFile f (cfgFile);
QStringList list;