UltraScan III
us_admin.cpp
Go to the documentation of this file.
1 #include "us_admin.h"
3 #include "us_license_t.h"
4 #include "us_help.h"
5 #include "us_settings.h"
6 #include "us_gui_settings.h"
7 #include "us_crypto.h"
8 
9 US_Admin::US_Admin( QWidget* w, Qt::WindowFlags flags )
10  : US_Widgets( true, w, flags )
11 {
12  setWindowTitle( "Change Master Password" );
13  setAttribute( Qt::WA_DeleteOnClose );
14 
15  int buttonh = 26;
16 
17  setPalette( US_GuiSettings::frameColor() );
18 
19  QLabel* header = us_banner( "Change Master Password" );
20  header->setMinimumHeight( buttonh * 3 / 2 );
21 
22  QString oldPass = US_Settings::UltraScanPW();
23  QLabel* oldPW = NULL;
24 
25  if ( ! oldPass.isEmpty() )
26  {
27  oldPW = us_label( "Old Password:" );
28  oldPW->setMinimumHeight( buttonh );
29 
30  le_oldPasswd = us_lineedit( "" );
31  le_oldPasswd->setEchoMode( QLineEdit::Password );
32  le_oldPasswd->setMinimumHeight( buttonh );
33  }
34  else
35  le_oldPasswd = NULL;
36 
37  QLabel* passwd1 = us_label( "Enter New Password:" );
38  passwd1->setMinimumHeight( buttonh );
39 
40  le_passwd1 = us_lineedit( "" );
41  le_passwd1->setEchoMode( QLineEdit::Password );
42  le_passwd1->setMinimumHeight( buttonh );
43  le_passwd1->setMinimumWidth( 300 );
44 
45  QLabel* passwd2 = us_label( "Verify New Password:" );
46  passwd2->setMinimumHeight(buttonh);
47 
48  le_passwd2 = us_lineedit( "" );
49  le_passwd2->setMinimumHeight( buttonh );
50  le_passwd2->setEchoMode( QLineEdit::Password );
51 
52  pb_help = us_pushbutton( "Help" );
53  pb_help->setMinimumHeight( buttonh );
54  connect( pb_help, SIGNAL( clicked() ), SLOT( help() ) );
55 
56  pb_save = us_pushbutton( "Save" );
57  pb_save->setMinimumHeight( buttonh );
58  connect( pb_save, SIGNAL( clicked() ), SLOT( save() ) );
59 
60  pb_cancel = us_pushbutton( "Close" );
61  pb_cancel->setMinimumHeight( buttonh );
62  connect( pb_cancel, SIGNAL( clicked() ), SLOT( close() ) );
63 
64  // Layout
65  QGridLayout* passwords = new QGridLayout;
66  passwords->addWidget( header, 0, 0, 1, 2 );
67 
68  int row = 1;
69  if ( ! oldPass.isEmpty() )
70  {
71  passwords->addWidget( oldPW , row, 0 );
72  passwords->addWidget( le_oldPasswd, row++, 1 );
73  }
74 
75  passwords->addWidget( passwd1 , row, 0 );
76  passwords->addWidget( le_passwd1, row++, 1 );
77  passwords->addWidget( passwd2 , row, 0 );
78  passwords->addWidget( le_passwd2, row++, 1 );
79 
80  QHBoxLayout* buttonLine = new QHBoxLayout;
81  buttonLine->addWidget( pb_help );
82  buttonLine->addWidget( pb_save );
83  buttonLine->addWidget( pb_cancel );
84 
85  QVBoxLayout* main = new QVBoxLayout;
86  main->setSpacing( 2 );
87  main->addLayout( passwords );
88  main->addLayout( buttonLine );
89 
90  setLayout( main );
91 }
92 
93 void US_Admin::save( void )
94 {
95  QByteArray oldPW = US_Settings::UltraScanPW();
96 
97  if ( ! oldPW.isEmpty() )
98  {
99  QByteArray calcsha1 =
100  QCryptographicHash::hash( le_oldPasswd->text().toAscii(),
101  QCryptographicHash::Sha1 );
102 
103  if ( calcsha1 != oldPW )
104  {
105  QMessageBox::information( this,
106  tr( "Attention:" ),
107  tr( "The old password is incorrect. Please re-input.\n" ) );
108  return;
109  }
110  }
111 
112  if ( le_passwd1->text() != le_passwd2->text() )
113  {
114  le_passwd1->setText("");
115  le_passwd2->setText("");
116 
117  QMessageBox::information( this,
118  tr( "Attention:" ),
119  tr( "The entered passwords are not same. Please re-input.\n" ) );
120  return;
121  }
122 
123  QString newPW = le_passwd1->text();
124 
125  if ( newPW.isEmpty() )
126  {
127  QMessageBox::information( this,
128  tr( "Attention:" ),
129  tr( "The new password cannot be empty. Please re-input.\n" ) );
130  return;
131  }
132 
134 
135  QByteArray sha1string =
136  QCryptographicHash::hash( newPW.toAscii(), QCryptographicHash::Sha1 );
137 
138  US_Settings::set_UltraScanPW( sha1string );
139 
140  // We need to reset any passwords in databases here
141  // from le_oldPasswd->text()
142  // to le_passwd1->text()
143 
144  QStringList defaultDB = US_Settings::defaultDB();
145  QString oldPass;
146 
147  if ( le_oldPasswd != NULL ) oldPass = le_oldPasswd->text();
148 
149  if ( defaultDB.size() > 0 )
150  {
151  // Decrypt with old password
152  // 4 = cipher; 5 = initialization vector
153  QString db_password = US_Crypto::decrypt( defaultDB.at( 4 ), oldPass,
154  defaultDB.at( 5 ) );
155 
156  // Encrypt with new password
157  QStringList cipherText = US_Crypto::encrypt( db_password, newPW );
158 
159  defaultDB.replace( 4, cipherText.at( 0 ) );
160  defaultDB.replace( 5, cipherText.at( 1 ) );
161 
162  US_Settings::set_defaultDB( defaultDB );
163  }
164 
165  QList<QStringList> databases = US_Settings::databases();
166 
167  for ( int i = 0; i < databases.size(); i++ )
168  {
169  QStringList database = databases.at( i );
170 
171  QString db_password = US_Crypto::decrypt( database.at( 4 ), oldPass,
172  database.at( 5 ) );
173 
174  QStringList cipherText = US_Crypto::encrypt( db_password, newPW );
175 
176  database.replace( 4, cipherText.at( 0 ) );
177  database.replace( 5, cipherText.at( 1 ) );
178 
179  databases.replace( i, database );
180  }
181 
182  if ( databases.size() > 0 )
183  US_Settings::set_databases( databases );
184 
185  g.setPasswd( newPW );
186  close();
187 }
188 
190 {
191  US_Help* online_help = new US_Help( this );
192  online_help->show_help( "manual/administrator.html" );
193 }
194