-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqinputdbpassword.cpp
More file actions
279 lines (256 loc) · 8.7 KB
/
qinputdbpassword.cpp
File metadata and controls
279 lines (256 loc) · 8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "qinputdbpassword.h"
#include "ui_qinputdbpassword.h"
#include "Utility.h"
#include "QWaitCursor.h"
#include "WinService.h"
#include <QMessageBox>
#include <QSettings>
#include <commctrl.h>
#include <QTcpSocket>
#include "MySQLAgent.h"
QInputDBPassword::QInputDBPassword(QString strMySQLPath,unsigned short nMySQLPort,QWidget *parent) :
QDialog(parent),
m_strMySQLPath(strMySQLPath),
m_nMySQLPort(nMySQLPort),
ui(new Ui::QInputDBPassword)
{
ui->setupUi(this);
QStringList arguments = QApplication::arguments();
//if (arguments.size()< 2)
//{
// ui->pushButton_ResetPassword->setEnabled(false);
//}
//else
// m_strMySQLService = arguments[1];
Button_SetElevationRequiredState(HWND(ui->pushButton_ResetPassword->winId()), TRUE);
}
QInputDBPassword::~QInputDBPassword()
{
delete ui;
}
void QInputDBPassword::on_pushButton_OK_clicked()
{
m_strPassword = ui->lineEdit_Password->text();
QDialog::accept();
}
void QInputDBPassword::on_pushButton_Cancel_clicked()
{
QDialog::reject();
}
bool QInputDBPassword::ResetMySQLPassword(QString strNewPassword,QString &strMessage)
{
try
{
QString strPassword = ui->lineEdit_NewPassword->text();
SERVICE_STATUS_PROCESS ssStatus;
int nResult = 0;
char szError[1024] = { 0 };
CMySQLAgent DBConnector;
nResult = DBConnector.Connect("127.0.0.1", "root", ""); // Access denied,may Account or password error
if (!nResult) // return a non zero value indicated a failure!
{
try
{
ULONGLONG nAffect = DBConnector.ExecuteSQL("update mysql.user set mysql.user.password=password('%s') where mysql.user.user='root'",strNewPassword.toStdString().c_str());
return true;
}
catch (CMySQLException e)
{
strMessage = e.what();
return false;
}
}
else
{
strMessage = DBConnector.GetErrorMsg();
return false;
}
}
catch (std::exception& e)
{
strMessage = e.what();
return false;
//QMessageBox::critical(nullptr, tr("Exception"), e.what(), QMessageBox::Ok);
}
}
void QInputDBPassword::on_pushButton_ResetPassword_clicked()
{
try
{
if (QMessageBox::warning(nullptr,
tr("Warning"),
tr("MySQL Service will be restarted while resetting password,Press yes to to continue!"),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
return;
QWaitCursor Wait;
QString strNewPassword = ui->lineEdit_NewPassword->text();
if (strNewPassword.size() < 6)
{
Wait.Restore();
QMessageBox::information(nullptr, tr("Information"), tr("To Reset password,you have to enter a new password that not less than 6 characters! "), QMessageBox::Ok);
return;
}
QString strMessage;
ui->progressBar->setRange(1, 100);
ui->progressBar->setValue(1);
ui->progressBar->setFormat(tr("Searching system services..."));
bool bServiceStop = false;
std::string strMySQLService = "";
ServiceInformationArray SvrConfigArray;
ServiceMgr.GetAllServiceInformation(SvrConfigArray);
int nProgress = 5;
ui->progressBar->setValue(nProgress);
if (SvrConfigArray.size())
{
ui->progressBar->setFormat(tr("[5%]Try Match MySQL Service..."));
QString strBinPath = m_strMySQLPath + "\\bin\\Mysqld.exe";
char szBinPath[1024] = { 0 };
strcpy(szBinPath, m_strMySQLPath.toStdString().c_str());
auto itFind = find_if(SvrConfigArray.begin(), SvrConfigArray.end(), [szBinPath](ServiceInformationPtr p)
{
return strstr(p->ServiceConfig.lpBinaryPathName, szBinPath) != nullptr;
});
// if the MySQL Service is installed and it's Prescess path equal to m_strMySQLPath,and it is running ,then stop it !
if (itFind != SvrConfigArray.end())
{
strMySQLService = (*itFind)->EnumServiceStatus.lpServiceName;
nProgress = 15;
ui->progressBar->setValue(nProgress);
ui->progressBar->setFormat(QString(tr("[1%]Succeed in matching MySQL Service,Try to stop it...")).arg(nProgress));
if ((*itFind)->EnumServiceStatus.ServiceStatus.dwCurrentState == Running)
{
SERVICE_STATUS_PROCESS ssStatus;
int nResult = ServiceMgr.Stop((*itFind)->EnumServiceStatus.lpServiceName, ssStatus);
if (nResult )
{
char szError[1024] = { 0 };
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
nResult,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPSTR)szError,
1024,
NULL);
strMessage = QString("Failed to stop Service %1:%2").arg((*itFind)->EnumServiceStatus.lpServiceName).arg(szError);
ui->progressBar->setFormat(tr("Operation aborted!"));
ui->progressBar->setValue(100);
Wait.Restore();
QMessageBox::critical(nullptr, tr("Information"), strMessage, QMessageBox::Abort);
}
bServiceStop = true;
}
nProgress = 30;
ui->progressBar->setFormat(QString(tr("[%1%]MySQL Service has been stopped,Try to Update Password...!")).arg(nProgress));
ui->progressBar->setValue(nProgress);
}
else
{
nProgress = 30;
ui->progressBar->setFormat(QString(tr("[30%]Failed matching MySQL Service,Try to Update Password...")).arg(nProgress));
ui->progressBar->setValue(nProgress);
}
}
/*if (!ShutDownMySQL(m_strMySQLPath))
{
QMessageBox::information(nullptr, tr("Information"), tr("Failed Stop MySQL Service ,can't find file MySQLAdmin.exe!"), QMessageBox::Ok);
return;
}*/
StartMySQLWithoutAuthenticaion();
nProgress = 40;
ui->progressBar->setValue(nProgress);
if (ResetMySQLPassword(strNewPassword, strMessage))
{
nProgress = 45;
ui->progressBar->setValue(nProgress);
ui->progressBar->setFormat(QString(tr("[1%]Succeed in updating MySQL Password,Try to clean up...")).arg(nProgress));
ShutDownMySQL(m_strMySQLPath);
nProgress = 50;
ui->progressBar->setValue(nProgress);
ui->progressBar->setFormat(QString(tr("[%1%]Clean up is Completed,Try to restart MySQL Service...")).arg(nProgress));
if (strMySQLService.size() && bServiceStop)
{
DWORD dwT1 = timeGetTime();
int nProgress = 50;
DWORD dwWaitTime = 4000;
int nLastDiff = 0;
DWORD dwT2 = 0;
while (true)
{
dwT2 = timeGetTime();
if ((dwT2- dwT1) >= dwWaitTime)
break;
else
{
int nDiff = (dwT2 - dwT1) / 100;
if (nDiff > nLastDiff)
{
//qDebug("%s Progress = %d.\n", __FUNCTION__, 50 + nDiff);
ui->progressBar->setValue(50 + nDiff);
ui->progressBar->setFormat(QString(tr("[%1%]Clean up is Completed,Try to restart MySQL Service...")).arg(50 + nDiff));
nLastDiff = nDiff;
}
Sleep(50);
}
}
SERVICE_STATUS_PROCESS ssStatus;
int nResult = ServiceMgr.Start(strMySQLService.c_str(),ssStatus);
char szError[1024] = { 0 };
if (nResult || ssStatus.dwCurrentState != Running)
{
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
nResult,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPSTR)szError,
1024,
NULL);
strMessage = QString(tr("Failed in restarting Service '%1':%2")).arg(strMySQLService.c_str()).arg(szError);
Wait.Restore();
QMessageBox::critical(nullptr, tr("Information"), strMessage, QMessageBox::Abort);
return ;
}
nProgress = 95;
ui->progressBar->setValue(nProgress);
ui->progressBar->setFormat(QString(tr("[%1%]MySQL Service has been started...")).arg(nProgress));
CMySQLAgent DBConnector;
nResult = DBConnector.Connect("127.0.0.1", "root", strNewPassword.toStdString().c_str());
if (nResult) // return a non zero value indicated a failure!
{
strMessage = DBConnector.GetErrorMsg();
return ;
}
}
ui->lineEdit_Password->setText(strNewPassword);
ui->progressBar->setValue(100);
ui->progressBar->setFormat(tr("The password has been reset!"));
Wait.Restore();
QMessageBox::information(nullptr, tr("Information"), tr("The password has been reset! "), QMessageBox::Ok);
}
else
{
Wait.Restore();
QMessageBox::warning(nullptr, tr("Information"), strMessage, QMessageBox::Ok);
}
}
catch (DWORD errCode)
{
TraceMsgA("%s Exception code:%d", __FUNCTION__, errCode);
}
}
void QInputDBPassword::on_checkBox_HidePassword_stateChanged(int arg1)
{
if (arg1 == 0)
{
ui->lineEdit_NewPassword->setEchoMode(QLineEdit::Normal);
ui->lineEdit_Password->setEchoMode(QLineEdit::Normal);
}
else
{
ui->lineEdit_NewPassword->setEchoMode(QLineEdit::Password);
ui->lineEdit_Password->setEchoMode(QLineEdit::Password);
}
}
QString QInputDBPassword::GetPassword()
{
return m_strPassword;
}