Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / ui / qt / tlskeylog_launcher_dialog.cpp
blob589e4a381a137a68df3de347e3aff8eff9e23811
1 /*
2 * tlskeylog_launcher_dialog.c
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
11 #include <config.h>
12 #include "tlskeylog_launcher_dialog.h"
13 #include <ui_tlskeylog_launcher_dialog.h>
15 #include "main_application.h"
16 #include "ui/qt/widgets/wireshark_file_dialog.h"
17 #include "wsutil/report_message.h"
18 #include <models/pref_models.h>
19 #include <epan/prefs-int.h>
20 #include <ui/preference_utils.h>
22 TLSKeylogDialog::TLSKeylogDialog(QWidget &parent) :
23 QDialog(&parent),
24 ui(new Ui::TLSKeylogDialog),
25 pref_tls_keylog_(nullptr),
26 pref_tlskeylog_command_(nullptr)
28 ui->setupUi(this);
30 QString title(tr("Launch application with SSLKEYLOGFILE"));
31 setWindowTitle(mainApp->windowTitleString(title));
33 QPushButton *launch_button = ui->buttonBox->addButton(tr("Launch"), QDialogButtonBox::ActionRole);
34 launch_button->setDefault(true);
35 connect(launch_button, &QPushButton::clicked, this, &TLSKeylogDialog::on_launchActivated);
37 QPushButton *save_button = ui->buttonBox->addButton(tr("Save"), QDialogButtonBox::ApplyRole);
38 connect(save_button, &QPushButton::clicked, this, &TLSKeylogDialog::on_saveActivated);
40 QPushButton *reset_button = ui->buttonBox->button(QDialogButtonBox::Reset);
41 connect(reset_button, &QPushButton::clicked, this, &TLSKeylogDialog::on_resetActivated);
43 connect(ui->keylogPushButton, &QPushButton::clicked, this, &TLSKeylogDialog::on_browseKeylogPath);
44 connect(ui->programPushbutton, &QPushButton::clicked, this, &TLSKeylogDialog::on_browseProgramPath);
46 tls_module_ = prefs_find_module("tls");
47 if (tls_module_) {
48 pref_tls_keylog_ = prefs_find_preference(tls_module_, "keylog_file");
49 if (pref_tls_keylog_) {
50 const char *path = prefs_get_string_value(pref_tls_keylog_, pref_current);
51 if (path && *path) {
52 ui->keylogLineEdit->setText(QString(path));
57 gui_module_ = prefs_find_module("gui");
58 ws_assert(gui_module_);
59 pref_tlskeylog_command_ = prefs_find_preference(gui_module_, "tlskeylog_command");
60 ws_assert(pref_tlskeylog_command_);
61 const char *path = prefs_get_string_value(pref_tlskeylog_command_, pref_current);
62 if (path && *path) {
63 ui->commandLineEdit->setText(QString(path));
67 TLSKeylogDialog::~TLSKeylogDialog()
69 delete ui;
72 void TLSKeylogDialog::on_saveActivated()
74 int changed;
76 if (pref_tls_keylog_) {
77 QString keylog = ui->keylogLineEdit->text();
78 changed = prefs_set_string_value(pref_tls_keylog_, qUtf8Printable(keylog), pref_current);
79 tls_module_->prefs_changed_flags |= changed;
82 QString command = ui->commandLineEdit->text();
83 changed = prefs_set_string_value(pref_tlskeylog_command_, qUtf8Printable(command), pref_current);
84 gui_module_->prefs_changed_flags |= changed;
86 prefs_main_write();
89 void TLSKeylogDialog::on_launchActivated()
91 QProcess externalProcess;
92 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
94 QString keylog = ui->keylogLineEdit->text();
95 if (keylog.isEmpty())
96 return;
97 QString command = ui->commandLineEdit->text();
98 if (command.isEmpty())
99 return;
100 QStringList commandArgs = QProcess::splitCommand(command);
101 if (commandArgs.isEmpty())
102 return;
104 // This should work with command lines such as:
105 // - firefox
106 // - firefox -profile /tmp/ff
107 // - /usr/bin/firefox -profile /tmp/ff
108 // - "C:\Program Files\Mozilla Firefox\firefox.exe"
109 // - "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --user-data-dir=/tmp/cr
110 externalProcess.setProgram(commandArgs.takeFirst());
111 externalProcess.setArguments(commandArgs);
113 env.insert("SSLKEYLOGFILE", keylog);
114 externalProcess.setProcessEnvironment(env);
115 bool ok = externalProcess.startDetached();
116 if (ok) {
117 return;
120 QString error = externalProcess.errorString();
121 if (!error.isEmpty())
122 report_failure("Error launching command: %s", qUtf8Printable(error));
123 else
124 report_failure("Error launching command");
127 // Restore user preferences
128 void TLSKeylogDialog::on_resetActivated()
130 QString keylog_path;
131 QString tlskeylog_command;
133 if (pref_tls_keylog_) {
134 keylog_path = prefs_get_string_value(pref_tls_keylog_, pref_current);
135 ui->keylogLineEdit->setText(keylog_path);
137 tlskeylog_command = prefs_get_string_value(pref_tlskeylog_command_, pref_current);
138 ui->commandLineEdit->setText(tlskeylog_command);
141 void TLSKeylogDialog::on_browseKeylogPath()
143 QString caption = mainApp->windowTitleString(tr("TLS Keylog file"));
144 QString file_name = WiresharkFileDialog::getSaveFileName(this, caption,
145 mainApp->openDialogInitialDir().path());
146 if (!file_name.isEmpty()) {
147 ui->keylogLineEdit->setText(file_name);
151 void TLSKeylogDialog::on_browseProgramPath()
153 QString caption = mainApp->windowTitleString(tr("Program to start with SSLKEYLOGFILE"));
154 QString file_name = WiresharkFileDialog::getOpenFileName(this, caption);
155 if (file_name.isEmpty()) {
156 return;
158 #ifdef Q_OS_MAC
159 if (file_name.endsWith(".app")) {
160 QString base_name = QFileInfo(file_name).baseName();
161 QString bundle_exe_name = QStringLiteral("%1/Contents/MacOS/%2").arg(file_name, base_name);
162 if (QFile::exists(bundle_exe_name)) {
163 file_name = bundle_exe_name;
166 #endif
167 // If the program contains spaces, quote it to ensure it is not broken up
168 // into multiple arguments.
169 if (file_name.contains(" ")) {
170 file_name = QStringLiteral("\"%1\"").arg(file_name);
172 ui->commandLineEdit->setText(file_name);