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
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
) :
24 ui(new Ui::TLSKeylogDialog
),
25 pref_tls_keylog_(nullptr),
26 pref_tlskeylog_command_(nullptr)
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");
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
);
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
);
63 ui
->commandLineEdit
->setText(QString(path
));
67 TLSKeylogDialog::~TLSKeylogDialog()
72 void TLSKeylogDialog::on_saveActivated()
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
;
89 void TLSKeylogDialog::on_launchActivated()
91 QProcess externalProcess
;
92 QProcessEnvironment env
= QProcessEnvironment::systemEnvironment();
94 QString keylog
= ui
->keylogLineEdit
->text();
97 QString command
= ui
->commandLineEdit
->text();
98 if (command
.isEmpty())
100 QStringList commandArgs
= QProcess::splitCommand(command
);
101 if (commandArgs
.isEmpty())
104 // This should work with command lines such as:
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();
120 QString error
= externalProcess
.errorString();
121 if (!error
.isEmpty())
122 report_failure("Error launching command: %s", qUtf8Printable(error
));
124 report_failure("Error launching command");
127 // Restore user preferences
128 void TLSKeylogDialog::on_resetActivated()
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()) {
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
;
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
);