4545# -> org.freedesktop.NetworkManager.Settings (Settings Profile Manager)
4646
4747import asyncio
48+ import binascii
4849import functools
4950import logging
50- import pprint
5151import sdbus
5252from uuid import uuid4
5353from argparse import ArgumentParser , Namespace
54- from sdbus_async .networkmanager import NetworkManagerSettings
55- from sdbus_async .networkmanager import NetworkManagerConnectionProperties
54+ from passlib .utils .pbkdf2 import pbkdf2 # type: ignore
55+ from pprint import pformat
56+ from sdbus_async .networkmanager import (
57+ NetworkManagerSettings as SettingsManager ,
58+ ConnectionProfile ,
59+ ConnectionSettings ,
60+ ConnectionType ,
61+ Ipv4Settings ,
62+ Ipv6Settings ,
63+ WirelessSettings ,
64+ WirelessSecuritySettings ,
65+ )
5666
5767
5868async def add_wifi_psk_connection_async (args : Namespace ) -> str :
@@ -63,45 +73,44 @@ async def add_wifi_psk_connection_async(args: Namespace) -> str:
6373 info = logging .getLogger ().info
6474
6575 # If we add many connections passing the same id, things get messy. Check:
66- if await NetworkManagerSettings ().get_connections_by_id (args .conn_id ):
76+ if await SettingsManager ().get_connections_by_id (args .conn_id ):
6777 print (f'Connection "{ args .conn_id } " exists, remove it first' )
6878 print (f'Run: nmcli connection delete "{ args .conn_id } "' )
6979 return ""
7080
71- properties : NetworkManagerConnectionProperties = {
72- "connection" : {
73- "id" : ("s" , args .conn_id ),
74- "uuid" : ("s" , str (args .uuid )),
75- "type" : ("s" , "802-11-wireless" ),
76- "autoconnect" : ("b" , bool (hasattr (args , "auto" ) and args .auto )),
77- },
78- "802-11-wireless" : {
79- "mode" : ("s" , "infrastructure" ),
80- "security" : ("s" , "802-11-wireless-security" ),
81- "ssid" : ("ay" , args .ssid .encode ("utf-8" )),
82- },
83- "802-11-wireless-security" : {
84- "key-mgmt" : ("s" , "wpa-psk" ),
85- "auth-alg" : ("s" , "open" ),
86- "psk" : ("s" , args .psk ),
87- },
88- "ipv4" : {"method" : ("s" , "auto" )},
89- "ipv6" : {"method" : ("s" , "auto" )},
90- }
81+ if args .key_mgmt == "wpa-psk" and len (args .password ) < 64 :
82+ # Hash the password into a psk hash to not store it in clear form:
83+ pw = pbkdf2 (args .password .encode (), args .ssid .encode (), 4096 , 32 )
84+ args .password = binascii .hexlify (pw ).decode ("utf-8" )
85+
86+ profile = ConnectionProfile (
87+ connection = ConnectionSettings (
88+ connection_id = args .conn_id ,
89+ uuid = str (args .uuid ),
90+ connection_type = ConnectionType .WIFI .value ,
91+ autoconnect = bool (hasattr (args , "auto" ) and args .auto ),
92+ ),
93+ ipv4 = Ipv4Settings (method = "auto" ),
94+ ipv6 = Ipv6Settings (method = "auto" ),
95+ wireless = WirelessSettings (ssid = args .ssid .encode ("utf-8" )),
96+ wireless_security = WirelessSecuritySettings (
97+ key_mgmt = args .key_mgmt , auth_alg = "open" , psk = args .password
98+ ),
99+ )
91100
92101 # To bind the new connection to a specific interface, use this:
93102 if hasattr (args , "interface_name" ) and args .interface_name :
94- properties [ " connection" ][ "interface-name" ] = ( "s" , args .interface_name )
103+ profile . connection . interface_name = args .interface_name
95104
96- s = NetworkManagerSettings ()
105+ s = SettingsManager ()
97106 save = bool (hasattr (args , "save" ) and args .save )
98107 addconnection = s .add_connection if save else s .add_connection_unsaved
99- connection_settings_dbus_path = await addconnection (properties )
108+ connection_settings_dbus_path = await addconnection (profile . to_dbus () )
100109 created = "created and saved" if save else "created"
101110 info (f"New unsaved connection profile { created } , show it with:" )
102111 info (f'nmcli connection show "{ args .conn_id } "|grep -v -e -- -e default' )
103112 info ("Settings used:" )
104- info (functools .partial (pprint . pformat , sort_dicts = False )(properties ))
113+ info (functools .partial (pformat , sort_dicts = False )(profile . to_settings_dict () ))
105114 return connection_settings_dbus_path
106115
107116
@@ -112,7 +121,8 @@ async def add_wifi_psk_connection_async(args: Namespace) -> str:
112121 p .add_argument ("-c" , dest = "conn_id" , default = conn_id , help = "Connection Id" )
113122 p .add_argument ("-u" , dest = "uuid" , default = uuid4 (), help = "Connection UUID" )
114123 p .add_argument ("-s" , dest = "ssid" , default = "CafeSSID" , help = "WiFi SSID" )
115- p .add_argument ("-p" , dest = "psk" , default = "Coffee!!" , help = "WiFi PSK" )
124+ p .add_argument ("-k" , dest = "key_mgmt" , default = "wpa-psk" , help = "key-mgmt" )
125+ p .add_argument ("-p" , dest = "password" , default = "Coffee!!" , help = "WiFi PSK" )
116126 p .add_argument ("-i" , dest = "interface_name" , default = "" , help = "WiFi device" )
117127 p .add_argument ("-a" , dest = "auto" , action = "store_true" , help = "autoconnect" )
118128 p .add_argument ("--save" , dest = "save" , action = "store_true" , help = "Save" )
0 commit comments