Linux distros that use NetworkManager: prefill WLAN PSK

Only supported when NetworkManager is used on the Linux distro
as wpa_supplicant itself does not support reading PSKs through
DBus, only setting them.

Closes #457
This commit is contained in:
Floris Bos 2022-07-31 13:17:08 +02:00
parent b1606dd1fb
commit ef4abc661f
5 changed files with 107 additions and 3 deletions

View file

@ -0,0 +1,59 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2022 Raspberry Pi Ltd
*/
#include "networkmanagerapi.h"
#include <QDBusMetaType>
#include <QDBusInterface>
#include <QDBusReply>
#include <QDebug>
typedef QMap<QString, QVariantMap> VariantMapMap;
Q_DECLARE_METATYPE(VariantMapMap)
NetworkManagerApi::NetworkManagerApi(QObject *parent)
: QObject{parent}
{
}
QString NetworkManagerApi::getPSK()
{
qDBusRegisterMetaType<VariantMapMap>();
QDBusInterface nm("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager",
"org.freedesktop.NetworkManager", QDBusConnection::systemBus());
if (!nm.isValid())
{
qDebug() << "NetworkManager not available";
return QString();
}
const auto activeConnections = nm.property("ActiveConnections").value<QList<QDBusObjectPath>>();
for (const QDBusObjectPath &activeConnection : activeConnections)
{
QString activeConnectionPath = activeConnection.path();
if (activeConnectionPath.isEmpty())
continue;
QDBusInterface ac("org.freedesktop.NetworkManager", activeConnectionPath,
"org.freedesktop.NetworkManager.Connection.Active", QDBusConnection::systemBus());
if (!ac.isValid())
continue;
QString settingsPath = ac.property("Connection").value<QDBusObjectPath>().path();
if (settingsPath.isEmpty())
continue;
QDBusInterface settings("org.freedesktop.NetworkManager", settingsPath,
"org.freedesktop.NetworkManager.Settings.Connection", QDBusConnection::systemBus());
if (!settings.isValid())
continue;
QDBusReply<VariantMapMap> reply = settings.call("GetSecrets", "802-11-wireless-security");
if (!reply.isValid())
continue;
QVariantMap secrets = reply.value().value("802-11-wireless-security");
if (secrets.contains("psk"))
return secrets.value("psk").toString();
}
return QString();
}

View file

@ -0,0 +1,22 @@
#ifndef NETWORKMANAGERAPI_H
#define NETWORKMANAGERAPI_H
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2022 Raspberry Pi Ltd
*/
#include <QObject>
class NetworkManagerApi : public QObject
{
Q_OBJECT
public:
explicit NetworkManagerApi(QObject *parent = nullptr);
QString getPSK();
signals:
};
#endif // NETWORKMANAGERAPI_H