1+ use models:: ConnectionOptions ;
12use std:: collections:: HashMap ;
23use zvariant:: Value ;
34
4- use crate :: models;
5+ use crate :: models:: { self , EapMethod } ;
56
6- /* fn bytes(val: &str) -> Vec<u8> {
7+ fn bytes ( val : & str ) -> Vec < u8 > {
78 val. as_bytes ( ) . to_vec ( )
89}
910
11+ fn string_array ( xs : & [ & str ] ) -> Value < ' static > {
12+ let vals: Vec < String > = xs. iter ( ) . map ( |s| s. to_string ( ) ) . collect ( ) ;
13+ Value :: from ( vals)
14+ }
15+
1016fn base_wifi_section ( ssid : & str ) -> HashMap < & ' static str , Value < ' static > > {
1117 let mut s = HashMap :: new ( ) ;
1218 s. insert ( "ssid" , Value :: from ( bytes ( ssid) ) ) ;
1319 s. insert ( "mode" , Value :: from ( "infrastructure" ) ) ;
1420 s
1521}
1622
17- fn base_connection_section(ssid: &str) -> HashMap<&'static str, Value<'static>> {
23+ fn base_connection_section (
24+ ssid : & str ,
25+ opts : & ConnectionOptions ,
26+ ) -> HashMap < & ' static str , Value < ' static > > {
1827 let mut s = HashMap :: new ( ) ;
1928 s. insert ( "type" , Value :: from ( "802-11-wireless" ) ) ;
2029 s. insert ( "id" , Value :: from ( ssid. to_string ( ) ) ) ;
2130 s. insert ( "uuid" , Value :: from ( uuid:: Uuid :: new_v4 ( ) . to_string ( ) ) ) ;
22- s.insert("autoconnect", Value::from(true));
31+ s. insert ( "autoconnect" , Value :: from ( opts. autoconnect ) ) ;
32+
33+ if let Some ( p) = opts. autoconnect_priority {
34+ s. insert ( "autoconnect-priority" , Value :: from ( p) ) ;
35+ }
36+
37+ if let Some ( r) = opts. autoconnect_retries {
38+ s. insert ( "autoconnect-retries" , Value :: from ( r) ) ;
39+ }
40+
2341 s
2442}
2543
2644fn build_psk_security ( psk : & str ) -> HashMap < & ' static str , Value < ' static > > {
2745 let mut sec = HashMap :: new ( ) ;
46+
2847 sec. insert ( "key-mgmt" , Value :: from ( "wpa-psk" ) ) ;
2948 sec. insert ( "psk" , Value :: from ( psk. to_string ( ) ) ) ;
30- // hardening maybe
31- // sec.insert("proto", Value::from(vec!["rsn"]));
32- // pairwise
33- // etc...
49+ sec. insert ( "psk-flags" , Value :: from ( 0u32 ) ) ; // 0 = agent-owned, provided during activation
50+ sec. insert ( "auth-alg" , Value :: from ( "open" ) ) ;
51+
52+ sec. insert ( "proto" , string_array ( & [ "rsn" ] ) ) ;
53+ sec. insert ( "pairwise" , string_array ( & [ "ccmp" ] ) ) ;
54+ sec. insert ( "group" , string_array ( & [ "ccmp" ] ) ) ;
55+
3456 sec
3557}
3658
@@ -42,17 +64,18 @@ fn build_eap_security(
4264) {
4365 let mut sec = HashMap :: new ( ) ;
4466 sec. insert ( "key-mgmt" , Value :: from ( "wpa-eap" ) ) ;
45- sec.insert("auth-alg", Value::from("OPEN "));
67+ sec. insert ( "auth-alg" , Value :: from ( "open " ) ) ;
4668 // same hardening tips as psk
4769 // proto, pairwise, group, etc.
4870
4971 // 802-1x
5072 let mut e1x = HashMap :: new ( ) ;
51- let eap_vec = match opts.method {
52- models::EapMethod::Peap => vec!["peap"],
53- models::EapMethod::Ttls => vec!["ttls"],
73+
74+ let eap_str = match opts. method {
75+ EapMethod :: Peap => "peap" ,
76+ EapMethod :: Ttls => "ttls" ,
5477 } ;
55- e1x.insert("eap", Value::from(eap_vec ));
78+ e1x. insert ( "eap" , string_array ( & [ eap_str ] ) ) ;
5679 e1x. insert ( "identity" , Value :: from ( opts. identity . clone ( ) ) ) ;
5780 e1x. insert ( "password" , Value :: from ( opts. password . clone ( ) ) ) ;
5881
@@ -86,67 +109,48 @@ fn build_eap_security(
86109pub fn build_wifi_connection (
87110 ssid : & str ,
88111 security : & models:: WifiSecurity ,
112+ opts : & ConnectionOptions ,
89113) -> HashMap < & ' static str , HashMap < & ' static str , Value < ' static > > > {
90114 let mut conn: HashMap < & ' static str , HashMap < & ' static str , Value < ' static > > > = HashMap :: new ( ) ;
91- conn.insert("connection", base_connection_section(ssid));
115+
116+ // base connections
117+ conn. insert ( "connection" , base_connection_section ( ssid, opts) ) ;
92118 conn. insert ( "802-11-wireless" , base_wifi_section ( ssid) ) ;
93119
120+ // Add IPv4 and IPv6 configuration to prevent state 60 stall
121+ // TODO: Expand upon auto/manual configuration options
122+ let mut ipv4 = HashMap :: new ( ) ;
123+ ipv4. insert ( "method" , Value :: from ( "auto" ) ) ;
124+ conn. insert ( "ipv4" , ipv4) ;
125+
126+ let mut ipv6 = HashMap :: new ( ) ;
127+ ipv6. insert ( "method" , Value :: from ( "auto" ) ) ;
128+ conn. insert ( "ipv6" , ipv6) ;
129+
94130 match security {
95131 models:: WifiSecurity :: Open => { }
96132
97133 models:: WifiSecurity :: WpaPsk { psk } => {
98- conn.insert("802-11-wireless-security", build_psk_security(psk.as_str()));
99- }
134+ // point wireless at security section
135+ if let Some ( w) = conn. get_mut ( "802-11-wireless" ) {
136+ w. insert ( "security" , Value :: from ( "802-11-wireless-security" ) ) ;
137+ }
100138
101- models::WifiSecurity::WpaEap { opts } => {
102- let (sec, e1x) = build_eap_security(&opts);
139+ let sec = build_psk_security ( psk) ;
103140 conn. insert ( "802-11-wireless-security" , sec) ;
104- conn.insert("802-1x", e1x);
105141 }
106- }
107- conn
108- }*/
109-
110- pub fn build_wifi_connection (
111- ssid : & str ,
112- security : & models:: WifiSecurity ,
113- ) -> HashMap < & ' static str , HashMap < & ' static str , zvariant:: Value < ' static > > > {
114- let mut conn = HashMap :: new ( ) ;
115-
116- let mut s_conn = HashMap :: new ( ) ;
117- s_conn. insert ( "type" , Value :: from ( "802-11-wireless" ) ) ;
118- s_conn. insert ( "id" , Value :: from ( ssid. to_string ( ) ) ) ;
119- s_conn. insert ( "uuid" , Value :: from ( uuid:: Uuid :: new_v4 ( ) . to_string ( ) ) ) ;
120- s_conn. insert ( "autoconnect" , Value :: from ( true ) ) ;
121- s_conn. insert ( "interface-name" , Value :: from ( "wlan0" ) ) ;
122- conn. insert ( "connection" , s_conn) ;
123142
124- let mut s_wifi = HashMap :: new ( ) ;
125- s_wifi. insert ( "ssid" , Value :: from ( ssid. as_bytes ( ) . to_vec ( ) ) ) ;
126- s_wifi. insert ( "mode" , Value :: from ( "infrastructure" ) ) ;
143+ models:: WifiSecurity :: WpaEap { opts } => {
144+ if let Some ( w) = conn. get_mut ( "802-11-wireless" ) {
145+ w. insert ( "security" , Value :: from ( "802-11-wireless-security" ) ) ;
146+ }
127147
128- match security {
129- models:: WifiSecurity :: Open => { }
130- models:: WifiSecurity :: WpaPsk { psk } => {
131- s_wifi. insert ( "security" , Value :: from ( "802-11-wireless-security" ) ) ;
132- let mut s_sec = HashMap :: new ( ) ;
133- s_sec. insert ( "key-mgmt" , Value :: from ( "wpa-psk" ) ) ;
134- s_sec. insert ( "auth-alg" , Value :: from ( "open" ) ) ;
135- s_sec. insert ( "psk" , Value :: from ( psk. to_string ( ) ) ) ;
136- conn. insert ( "802-11-wireless-security" , s_sec) ;
148+ let ( mut sec, e1x) = build_eap_security ( opts) ;
149+ sec. insert ( "auth-alg" , Value :: from ( "open" ) ) ;
150+ conn. insert ( "802-11-wireless-security" , sec) ;
151+ conn. insert ( "802-1x" , e1x) ;
137152 }
138- _ => { }
139153 }
140154
141- conn. insert ( "802-11-wireless" , s_wifi) ;
142-
143- let mut ipv4 = HashMap :: new ( ) ;
144- ipv4. insert ( "method" , Value :: from ( "auto" ) ) ;
145- conn. insert ( "ipv4" , ipv4) ;
146-
147- let mut ipv6 = HashMap :: new ( ) ;
148- ipv6. insert ( "method" , Value :: from ( "auto" ) ) ;
149- conn. insert ( "ipv6" , ipv6) ;
150-
151155 conn
152156}
0 commit comments