From e2296deb1a91b4e2ee84cbb13e4bc84053f77cd0 Mon Sep 17 00:00:00 2001 From: Floris Bos Date: Tue, 4 May 2021 22:51:08 +0200 Subject: [PATCH] Mac OS X: advanced options. offer to prefill system wifi PSK Offer to prefill the wifi password of the system running Imager, like we do on Windows. Ask user first if that is desired, as it does cause OS X to generate a prompt asking for admin password. --- OptionsPopup.qml | 4 ++++ imagewriter.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/OptionsPopup.qml b/OptionsPopup.qml index 992fe66..3908b9b 100644 --- a/OptionsPopup.qml +++ b/OptionsPopup.qml @@ -438,6 +438,10 @@ Popup { fieldWifiPassword.text = imageWriter.getPSK(fieldWifiSSID.text) if (fieldWifiPassword.text.length) { chkShowPassword.checked = false + if (Qt.platform.os == "osx") { + /* User indicated wifi must be prefilled */ + chkWifi.checked = true + } } } } diff --git a/imagewriter.cpp b/imagewriter.cpp index 0f7921f..a9f61b1 100644 --- a/imagewriter.cpp +++ b/imagewriter.cpp @@ -37,6 +37,8 @@ #endif #ifdef Q_OS_DARWIN #include +#include +#include #else #include "openssl/evp.h" #include "openssl/sha.h" @@ -883,10 +885,34 @@ QString ImageWriter::getPSK(const QString &ssid) return psk; +#else +#ifdef Q_OS_DARWIN + SecKeychainRef keychainRef; + QString psk; + QByteArray ssidAscii = ssid.toLatin1(); + + if (QMessageBox::question(nullptr, "", + tr("Would you like to prefill the wifi password from the system keychain?")) == QMessageBox::Yes) + { + if (SecKeychainOpen("/Library/Keychains/System.keychain", &keychainRef) == errSecSuccess) + { + UInt32 resultLen; + void *result; + if (SecKeychainFindGenericPassword(keychainRef, 0, NULL, ssidAscii.length(), ssidAscii.constData(), &resultLen, &result, NULL) == errSecSuccess) + { + psk = QByteArray((char *) result, resultLen); + SecKeychainItemFreeContent(NULL, result); + } + CFRelease(keychainRef); + } + } + + return psk; #else Q_UNUSED(ssid) return QString(); #endif +#endif } bool ImageWriter::getBoolSetting(const QString &key)