File indexing completed on 2024-07-14 03:58:06

0001 /*
0002     SPDX-FileCopyrightText: 2012 Jan Grulich <jgrulich@redhat.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include <arpa/inet.h>
0008 
0009 #include <NetworkManagerQt/AccessPoint>
0010 #include <NetworkManagerQt/Connection>
0011 #include <NetworkManagerQt/ConnectionSettings>
0012 #include <NetworkManagerQt/Device>
0013 #include <NetworkManagerQt/Ipv4Setting>
0014 #include <NetworkManagerQt/Manager>
0015 #include <NetworkManagerQt/WirelessDevice>
0016 #include <NetworkManagerQt/WirelessSecuritySetting>
0017 #include <NetworkManagerQt/WirelessSetting>
0018 
0019 #include <QDBusMetaType>
0020 #include <QTextStream>
0021 #include <QUuid>
0022 
0023 using namespace NetworkManager;
0024 
0025 int main()
0026 {
0027     qDBusRegisterMetaType<NMVariantMapMap>();
0028     QTextStream qout(stdout, QIODevice::WriteOnly);
0029     QTextStream qin(stdin, QIODevice::ReadOnly);
0030 
0031     NetworkManager::ConnectionSettings *settings = new NetworkManager::ConnectionSettings(NetworkManager::ConnectionSettings::Wireless);
0032     const Device::List deviceList = NetworkManager::networkInterfaces();
0033 
0034     WirelessDevice::Ptr wifiDevice;
0035 
0036     // We have to find some wireless device
0037     for (Device::Ptr dev : deviceList) {
0038         if (dev->type() == Device::Wifi) {
0039             wifiDevice = qobject_cast<WirelessDevice *>(dev);
0040             break;
0041         }
0042     }
0043 
0044     if (!wifiDevice) {
0045         return 1;
0046     }
0047 
0048     const QStringList accessPointList = wifiDevice->accessPoints();
0049     QString ssid;
0050     QString result;
0051     QString accessPointPath;
0052 
0053     // Check for available accesspoint
0054     for (const QString &ap : accessPointList) {
0055         AccessPoint accessPoint(ap);
0056         // For simplification we use APs only with Wep security or without any security
0057         /* clang-format off */
0058         if (accessPoint.wpaFlags().testFlag(AccessPoint::PairWep40)
0059             || accessPoint.wpaFlags().testFlag(AccessPoint::PairWep104)
0060             || accessPoint.wpaFlags().testFlag(AccessPoint::GroupWep40)
0061             || accessPoint.wpaFlags().testFlag(AccessPoint::GroupWep104)
0062             || !accessPoint.wpaFlags()) { /* clang-format on */
0063             qout << "Do you want to connect to " << accessPoint.ssid() << "?" << Qt::endl;
0064             qout << "Yes/No: " << Qt::flush;
0065             qin >> result;
0066 
0067             if (result.toLower() == "yes" || result == "y") {
0068                 ssid = accessPoint.ssid();
0069                 accessPointPath = ap;
0070                 break;
0071             }
0072         }
0073     }
0074 
0075     // Now we will prepare our new connection, we have to specify ID and create new UUID
0076     settings->setId(ssid);
0077     settings->setUuid(QUuid::createUuid().toString().mid(1, QUuid::createUuid().toString().length() - 2));
0078 
0079     // For wireless setting we have to specify SSID
0080     NetworkManager::WirelessSetting::Ptr wirelessSetting = settings->setting(Setting::Wireless).dynamicCast<WirelessSetting>();
0081     wirelessSetting->setSsid(ssid.toUtf8());
0082 
0083     NetworkManager::Ipv4Setting::Ptr ipv4Setting = settings->setting(Setting::Ipv4).dynamicCast<Ipv4Setting>();
0084     ipv4Setting->setMethod(NetworkManager::Ipv4Setting::Automatic);
0085 
0086     // Optional password setting. Can be skipped if you do not need encryption.
0087     NetworkManager::WirelessSecuritySetting::Ptr wifiSecurity =
0088         settings->setting(NetworkManager::Setting::WirelessSecurity).dynamicCast<NetworkManager::WirelessSecuritySetting>();
0089     wifiSecurity->setKeyMgmt(NetworkManager::WirelessSecuritySetting::WpaPsk);
0090     wifiSecurity->setPsk("12345678");
0091     wifiSecurity->setInitialized(true);
0092     wirelessSetting->setSecurity("802-11-wireless-security");
0093 
0094     // We try to add and activate our new wireless connection
0095     QDBusPendingReply<QDBusObjectPath, QDBusObjectPath> reply = NetworkManager::addAndActivateConnection(settings->toMap(), wifiDevice->uni(), accessPointPath);
0096 
0097     reply.waitForFinished();
0098 
0099     // Check if this connection was added successfuly
0100     if (reply.isValid()) {
0101         // Now our connection should be added in NetworkManager and we can print all settings pre-filled from NetworkManager
0102         NetworkManager::Connection connection(reply.value().path());
0103         NetworkManager::ConnectionSettings::Ptr newSettings = connection.settings();
0104         // Print resulting settings
0105         qDebug() << (*newSettings.data());
0106 
0107         // Continue with adding secrets
0108         NetworkManager::WirelessSecuritySetting::Ptr wirelessSecuritySetting =
0109             newSettings->setting(Setting::WirelessSecurity).dynamicCast<WirelessSecuritySetting>();
0110         if (!wirelessSecuritySetting->needSecrets().isEmpty()) {
0111             qDebug() << "Need secrets: " << wirelessSecuritySetting->needSecrets();
0112             // TODO: fill missing secrets
0113         }
0114 
0115     } else {
0116         qDebug() << "Connection failed: " << reply.error();
0117     }
0118 }