Verified Commit 536ed695 authored by Andre Heinecke's avatar Andre Heinecke
Browse files

Try to fill out name and email for new key on win

Under Windows we usually cannot rely on KEMailSettings so
we add a fallback that uses GetUserNameEx to query the
name and email through Windows API. This respects Active
Directory setups in most institutions and could in theory
be used to automate key generation.

GnuPG-Bug-Id: T5181
parent 377faf15
......@@ -337,6 +337,10 @@ if (COMPILE_WITH_UNITY_CMAKE_SUPPORT)
endif()
set_target_properties(kleopatra_bin PROPERTIES OUTPUT_NAME kleopatra)
if (WIN32)
set(_kleopatra_platform_libs "secur32")
endif ()
target_link_libraries(kleopatra_bin
Gpgmepp
QGpgme
......@@ -355,6 +359,7 @@ target_link_libraries(kleopatra_bin
${_kleopatra_uiserver_extra_libs}
${_kleopatra_dbusaddons_libs}
kleopatraclientcore
${_kleopatra_platform_libs}
)
install(TARGETS kleopatra_bin ${KDE_INSTALL_TARGETS_DEFAULT_ARGS})
......
......@@ -66,6 +66,12 @@
#include <KEMailSettings>
#include <QLocale>
#ifdef Q_OS_WIN
# include <windows.h>
# define SECURITY_WIN32
# include <secext.h> // For GetUserNameEx
#endif
using namespace Kleo;
using namespace Kleo::NewCertificateUi;
using namespace Kleo::Commands;
......@@ -266,6 +272,42 @@ static void parseAlgoString(const QString &algoString, int *size, Subkey::Pubkey
qCWarning(KLEOPATRA_LOG) << "Failed to parse default_pubkey_algo:" << algoString;
}
/* Use Windows API to query the user name and email.
EXTENDED_NAME_FORMAT is documented in MSDN */
#ifdef Q_OS_WIN
QString win_get_user_name (EXTENDED_NAME_FORMAT what)
{
QString ret;
wchar_t tmp[1];
ULONG nSize = 1;
if (what == NameUnknown) {
if (GetUserNameW (tmp, &nSize)) {
qCWarning (KLEOPATRA_LOG) << "Got empty username";
return ret;
}
} else if (GetUserNameExW (what, tmp, &nSize)) {
return ret;
}
/* nSize now contains the required size of the buffer */
wchar_t *buf = new wchar_t[nSize];
if (what == NameUnknown) {
if (!GetUserNameW (buf, &nSize)) {
qCWarning (KLEOPATRA_LOG) << "Failed to get username";
delete[] buf;
return ret;
}
} else if (!GetUserNameExW (what, buf, &nSize)) {
delete[] buf;
return ret;
}
ret = QString::fromWCharArray (buf, nSize);
delete[] buf;
return ret.trimmed();
}
#endif
Q_DECLARE_METATYPE(GpgME::Subkey::PubkeyAlgo)
namespace Kleo
{
......@@ -1434,10 +1476,25 @@ void EnterDetailsPage::updateForm()
const KEMailSettings e;
if (ui.nameLE->text().isEmpty()) {
ui.nameLE->setText(e.getSetting(KEMailSettings::RealName));
auto name = e.getSetting(KEMailSettings::RealName);
#ifdef Q_OS_WIN
if (name.isEmpty()) {
name = win_get_user_name(NameDisplay);
}
if (name.isEmpty()) {
name = win_get_user_name(NameUnknown);
}
#endif
ui.nameLE->setText(name);
}
if (ui.emailLE->text().isEmpty()) {
ui.emailLE->setText(e.getSetting(KEMailSettings::EmailAddress));
auto mbox = e.getSetting(KEMailSettings::EmailAddress);
#ifdef Q_OS_WIN
if (mbox.isEmpty()) {
mbox = win_get_user_name(NameUserPrincipal);
}
#endif
ui.emailLE->setText(mbox);
}
set_tab_order(widgets);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment