-
Notifications
You must be signed in to change notification settings - Fork 11
/
connection_options_dialog.cpp
330 lines (272 loc) · 10.9 KB
/
connection_options_dialog.cpp
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* ADMC - AD Management Center
*
* Copyright (C) 2020-2022 BaseALT Ltd.
* Copyright (C) 2020-2022 Dmitry Degtyarev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "connection_options_dialog.h"
#include "ui_connection_options_dialog.h"
#include "adldap.h"
#include "settings.h"
#include "utils.h"
#include "fsmo/fsmo_utils.h"
#include "globals.h"
#include "status.h"
#include <QPushButton>
#include <functional>
const QString CERT_STRATEGY_NEVER = CERT_STRATEGY_NEVER_define;
const QString CERT_STRATEGY_HARD = "hard";
const QString CERT_STRATEGY_DEMAND = "demand";
const QString CERT_STRATEGY_ALLOW = "allow";
const QString CERT_STRATEGY_TRY = "try";
ConnectionOptionsDialog::ConnectionOptionsDialog(QWidget *parent)
: QDialog(parent) {
ui = new Ui::ConnectionOptionsDialog();
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
const QList<QString> require_cert_list = {
CERT_STRATEGY_NEVER,
CERT_STRATEGY_HARD,
CERT_STRATEGY_DEMAND,
CERT_STRATEGY_ALLOW,
CERT_STRATEGY_TRY,
};
for (const QString &string : require_cert_list) {
ui->cert_combo->addItem(string);
}
// Load saved options
const int port = settings_get_variant(SETTING_port).toInt();
ui->port_spinbox->setValue(port);
const bool sasl_nocanon = settings_get_variant(SETTING_sasl_nocanon).toBool();
ui->canonize_check->setChecked(sasl_nocanon);
const QString cert_strategy = settings_get_variant(SETTING_cert_strategy).toString();
const int cert_strategy_index = ui->cert_combo->findText(cert_strategy);
ui->cert_combo->setCurrentIndex(cert_strategy_index);
default_domain = get_default_domain_from_krb5();
default_host_list = get_domain_hosts(default_domain, QString());
custom_domain = settings_get_variant(SETTING_custom_domain).toString();
custom_host_list = get_domain_hosts(custom_domain, QString());
// Populate hosts list
bool domain_is_default = settings_get_variant(SETTING_domain_is_default).toBool();
QStringList host_list;
QString domain;
if (domain_is_default) {
domain = default_domain;
host_list = default_host_list;
ui->host_default_button->setChecked(true);
ui->domain_custom_edit->setEnabled(false);
ui->domain_custom_edit->setText(default_domain);
ui->get_hosts_button->setVisible(false);
}
else {
domain = custom_domain;
host_list = custom_host_list;
ui->host_custom_button->setChecked(true);
ui->domain_custom_edit->setEnabled(true);
ui->domain_custom_edit->setText(custom_domain);
ui->get_hosts_button->setVisible(true);
}
any_hosts_available = !host_list.isEmpty();
if (any_hosts_available) {
ui->host_warning_label->setVisible(false);
for (const QString &host : host_list) {
ui->host_select_list->addItem(host);
}
set_saved_host_current_item();
} else {
ui->host_warning_label->setVisible(true);
}
settings_setup_dialog_geometry(SETTING_connection_options_dialog_geometry, this);
connect(ui->button_box->button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked,
this, &ConnectionOptionsDialog::load_default_options);
connect(ui->host_default_button, &QRadioButton::toggled,
this, &ConnectionOptionsDialog::host_button_toggled);
connect(ui->get_hosts_button, &QPushButton::clicked,
this, &ConnectionOptionsDialog::get_hosts);
}
ConnectionOptionsDialog::~ConnectionOptionsDialog() {
delete ui;
}
void ConnectionOptionsDialog::accept() {
if (any_hosts_available) {
const bool any_host_selected = !ui->host_select_list->selectedItems().isEmpty();
if (!any_host_selected) {
message_box_warning(this, tr("Error"), tr("Select a host."));
return;
}
}
const int port = ui->port_spinbox->value();
const bool sasl_nocanon = ui->canonize_check->isChecked();
const QString cert_strategy = ui->cert_combo->currentText();
QString selected_host;
QListWidgetItem *item = ui->host_select_list->currentItem();
if (item == nullptr) {
selected_host = QString();
}
else {
selected_host = item->text();
}
const bool domain_was_default = settings_get_variant(SETTING_domain_is_default).toBool();
const bool domain_is_default = ui->host_default_button->isChecked();
const bool custom_domain_changed = settings_get_variant(SETTING_custom_domain).toString() != custom_domain;
const QHash<QString, QVariant> settings = {
{SETTING_port, port},
{SETTING_sasl_nocanon, sasl_nocanon},
{SETTING_cert_strategy, cert_strategy},
{SETTING_host, selected_host},
{SETTING_custom_domain, custom_domain},
{SETTING_domain_is_default, domain_is_default}
};
load_connection_options(settings);
AdInterface ad;
if (ad_failed(ad, parentWidget())) {
// load back options from config
load_connection_options();
return;
}
for (const QString setting : settings.keys()) {
settings_set_variant(setting, settings.value(setting));
}
const bool domain_is_changed = (domain_was_default != domain_is_default) || custom_domain_changed;
if (domain_is_changed) {
load_g_adconfig(ad);
}
emit host_changed(selected_host);
if (!current_dc_is_master_for_role(ad, FSMORole_PDCEmulation)) {
if (gpo_edit_without_PDC_disabled)
g_status->add_message(tr("You are connected to DC without PDC-Emulator role. "
"Group policy editing is prohibited by the setting."), StatusType_Success);
else
g_status->add_message(tr("You are connected to DC without PDC-Emulator role. "
"Group policy editing is available."), StatusType_Success);
}
QDialog::accept();
}
void ConnectionOptionsDialog::load_default_options() {
ui->port_spinbox->setValue(0);
ui->canonize_check->setChecked(true);
const int never_index = ui->cert_combo->findText(CERT_STRATEGY_NEVER);
ui->cert_combo->setCurrentIndex(never_index);
if (any_hosts_available) {
ui->host_default_button->setChecked(true);
ui->host_select_list->setCurrentRow(0);
}
}
void ConnectionOptionsDialog::set_saved_host_current_item()
{
const QString saved_host = settings_get_variant(SETTING_host).toString();
if (!saved_host.isEmpty()) {
const QList<QListWidgetItem *> item_list = ui->host_select_list->findItems(saved_host, Qt::MatchExactly);
const bool saved_host_is_in_list = !item_list.isEmpty();
if (saved_host_is_in_list) {
QListWidgetItem *item = item_list[0];
ui->host_select_list->setCurrentItem(item);
}
} else {
// If saved host is empty, select first available
// host in select list
ui->host_select_list->setCurrentRow(0);
}
}
void ConnectionOptionsDialog::host_button_toggled(bool is_default_checked) {
ui->host_select_list->clear();
ui->host_warning_label->setVisible(false);
if (is_default_checked) {
ui->domain_custom_edit->setEnabled(false);
ui->domain_custom_edit->setText(default_domain);
ui->domain_custom_edit->setPlaceholderText(QString());
ui->get_hosts_button->setVisible(false);
if (default_host_list.isEmpty()) {
ui->host_warning_label->setVisible(true);
return;
}
else
ui->host_select_list->addItems(default_host_list);
}
else {
ui->domain_custom_edit->setEnabled(true);
ui->domain_custom_edit->setText(custom_domain);
ui->domain_custom_edit->setPlaceholderText("CUSTOM.DOMAIN.COM");
ui->get_hosts_button->setVisible(true);
if (custom_host_list.isEmpty()) {
ui->host_warning_label->setVisible(true);
return;
}
else
ui->host_select_list->addItems(custom_host_list);
}
set_saved_host_current_item();
}
void ConnectionOptionsDialog::get_hosts() {
ui->host_select_list->clear();
QString domain = ui->domain_custom_edit->text();
QStringList hosts = get_domain_hosts(domain, QString());
if (hosts.isEmpty()) {
ui->host_warning_label->setVisible(true);
}
else {
ui->host_warning_label->setVisible(false);
custom_domain = domain;
ui->host_select_list->addItems(hosts);
custom_host_list = hosts;
ui->host_select_list->setCurrentRow(0);
}
}
void load_connection_options(const QHash<QString, QVariant> &settings) {
std::function<QVariant(const QString &setting)> get_setting;
if (settings.isEmpty()) {
get_setting = [](const QString &setting) {
QVariant out = settings_get_variant(setting);
return out;
};
}
else {
get_setting = [settings](const QString &setting) {
QVariant out = settings[setting];
return out;
};
}
bool domain_is_default = true;
if (get_setting(SETTING_domain_is_default).isValid())
domain_is_default = get_setting(SETTING_domain_is_default).toBool();
AdInterface::set_domain_is_default(domain_is_default);
const QString custom_domain = get_setting(SETTING_custom_domain).toString();
AdInterface::set_custom_domain(custom_domain);
const QString saved_dc = get_setting(SETTING_host).toString();
AdInterface::set_dc(saved_dc);
const QVariant sasl_nocanon = get_setting(SETTING_sasl_nocanon);
if (sasl_nocanon.isValid()) {
AdInterface::set_sasl_nocanon(sasl_nocanon.toBool());
} else {
AdInterface::set_sasl_nocanon(true);
}
const QVariant port = get_setting(SETTING_port);
if (port.isValid()) {
AdInterface::set_port(port.toInt());
} else {
AdInterface::set_port(0);
}
const QString cert_strategy_string = get_setting(SETTING_cert_strategy).toString();
const QHash<QString, CertStrategy> cert_strategy_map = {
{CERT_STRATEGY_NEVER, CertStrategy_Never},
{CERT_STRATEGY_HARD, CertStrategy_Hard},
{CERT_STRATEGY_DEMAND, CertStrategy_Demand},
{CERT_STRATEGY_ALLOW, CertStrategy_Allow},
{CERT_STRATEGY_TRY, CertStrategy_Try},
};
const CertStrategy cert_strategy = cert_strategy_map.value(cert_strategy_string, CertStrategy_Never);
AdInterface::set_cert_strategy(cert_strategy);
}