Re: Help with getting saved wireless networks passwords with libnm

2020-11-09 Thread Beniamino Galvani via networkmanager-list
On Mon, Nov 09, 2020 at 12:41:21AM +0100, Flavio Collocola via 
networkmanager-list wrote:
> Hi all,
> 
> I'm trying to port my Windows program WiFi Password Recovery to Linux.
> WiFi Password Recovery is a simple tool which scans all wireless networks
> stored on the device and then returns their info (such as SSID, Auth
> algorithm, *password...*).
> To keep portability I'm developing it in C++ using wxWidgets library. In
> particular on Linux I'm trying to use libnm to interface the program with
> NetworkManager. Before starting the port I decided to write a small C
> console program to learn libnm API and NetworkManager basis.
> I'm trying to get the passwords of all wireless networks saved on Ubuntu
> using libnm but when i call the function *nm_setting_wireless_security_get_psk
> *it returns null. It doesn't work with debug dump functions of libnm too. I
> think I should do something with NMSecretAgentOld but I don't understand
> what exactly and how. I also did not find any example on the Internet. I
> slightly modified a C example about using libnm available on internet and
> this is my current test code:

Hi,

the remote connection doesn't contain any secrets. You should use
nm_remote_connection_get_secrets() or
nm_remote_connection_get_secrets_async() [1] to get them. The returned
GVariant is a dictionary where each key indicates the setting name and
the value is another dictionary mapping property names to the secret
value; for example:

{'802-11-wireless-security': {'psk': <'my-password'>}}

Note that retrieving secrets for a connection requires Polkit
permissions. If the connection has an owner (connection.permissions
property), the owner needs the "settings.modify.own" permission to get
secrets. Otherwise, the caller needs the "settings.modify.system"
permission. You can check permissions for the current user with
command: "nmcli general permissions".

[1] 
https://developer.gnome.org/libnm/stable/NMRemoteConnection.html#nm-remote-connection-get-secrets

Beniamino


signature.asc
Description: PGP signature
___
networkmanager-list mailing list
networkmanager-list@gnome.org
https://mail.gnome.org/mailman/listinfo/networkmanager-list


Help with getting saved wireless networks passwords with libnm

2020-11-09 Thread Flavio Collocola via networkmanager-list
Hi all,

I'm trying to port my Windows program WiFi Password Recovery to Linux.
WiFi Password Recovery is a simple tool which scans all wireless networks
stored on the device and then returns their info (such as SSID, Auth
algorithm, *password...*).
To keep portability I'm developing it in C++ using wxWidgets library. In
particular on Linux I'm trying to use libnm to interface the program with
NetworkManager. Before starting the port I decided to write a small C
console program to learn libnm API and NetworkManager basis.
I'm trying to get the passwords of all wireless networks saved on Ubuntu
using libnm but when i call the function *nm_setting_wireless_security_get_psk
*it returns null. It doesn't work with debug dump functions of libnm too. I
think I should do something with NMSecretAgentOld but I don't understand
what exactly and how. I also did not find any example on the Internet. I
slightly modified a C example about using libnm available on internet and
this is my current test code:

/*
* The example shows how to list connections.  Contrast this example with
* list-connections-gdbus.c, which is a bit lower level and talks directly to NM
* using GDBus.
*
* Compile with:
*   gcc -Wall test.cpp -o test `pkg-config --libs --cflags libnm`
*/
#include #include #include 
#include #include 
/* Print details of connection */static voidshow_connection
(NMConnection *connection){
   printf("--- CONNECTION ---\n");
   NMSettingWireless* s_con;
   NMSettingWirelessSecurity* wifiSec;
   const char* interfce;
   interfce = nm_connection_get_interface_name(connection);
   s_con = nm_connection_get_setting_wireless(connection);
   if (s_con) {
   GBytes* ssidBytes = nm_setting_wireless_get_ssid(s_con);
   const char* ssid = nm_utils_ssid_to_utf8((guint8
*)g_bytes_get_data(ssidBytes, NULL), g_bytes_get_size(ssidBytes));
   printf("SSID: %s\nInterface: %s\n", ssid, interfce);
   wifiSec = nm_connection_get_setting_wireless_security(connection);
   if(wifiSec){
   const char* auth =
nm_setting_wireless_security_get_auth_alg(wifiSec);
   const char* encrypt =
nm_setting_wireless_security_get_key_mgmt(wifiSec);
   const char* psk =
nm_setting_wireless_security_get_psk(wifiSec); //THIS RETURNS NULL!
BUT THE WPA-PSK PASSWORD IS SET!
   printf("Auth: %s\nEncryption: %s\nPassword: %s\n", auth,
encrypt, psk);
   nm_connection_dump(connection); //Also in debug dump no
password field
   }
   }
}
intmain (int argc, char *argv[]){
   NMClient *client;
   GError *error = NULL;
   const GPtrArray *connections;
   int i;
#if !GLIB_CHECK_VERSION (2, 35, 0)
   /* Initialize GType system */
   g_type_init ();#endif
   if (!(client = nm_client_new (NULL, ))) {
   g_message ("Error: Could not connect to NetworkManager: %s.",
error->message);
   g_error_free (error);
   return EXIT_FAILURE;
   }

   if (!nm_client_get_nm_running (client)) {
   g_message ("Error: Can't obtain connections: NetworkManager is
not running.");
   return EXIT_FAILURE;
   }
   /* Now the connections can be listed. */
   connections = nm_client_get_connections (client);

   printf ("Connections:\n===\n");

   for (i = 0; i < connections->len; i++)
   show_connection ((NMConnection*) connections->pdata[i]);

   g_object_unref (client);

   return EXIT_SUCCESS;
}

This is the interesting part of program output: Program output image


And this is the etc/NetworkManager/system_connections dump (where the
psk field is set): Wireless Network file dump image



Thanks in advance and Best Regards,


Flavio
___
networkmanager-list mailing list
networkmanager-list@gnome.org
https://mail.gnome.org/mailman/listinfo/networkmanager-list