1use serde::Serialize;
5
6use super::{Exposure, Ftp, MyFritz, RemoteAccess, Usp};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
10#[serde(rename_all = "snake_case")]
11pub enum Kind {
12 RemoteAccess,
13 Myfritz,
14 DynamicDns,
15 PortForwarding,
16 ExposedHost,
17 Upnp,
18 Ftp,
19 SharedLinks,
20 Vpn,
21 Tr069,
22 Usp,
23}
24
25impl Kind {
26 pub fn label(self) -> &'static str {
28 match self {
29 Self::RemoteAccess => "Remote access",
30 Self::Myfritz => "MyFRITZ",
31 Self::DynamicDns => "Dynamic DNS",
32 Self::PortForwarding => "Port forwarding",
33 Self::ExposedHost => "Exposed host",
34 Self::Upnp => "UPnP",
35 Self::Ftp => "Storage via FTP",
36 Self::SharedLinks => "Shared links",
37 Self::Vpn => "VPN",
38 Self::Tr069 => "TR-069",
39 Self::Usp => "USP",
40 }
41 }
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
46#[serde(rename_all = "lowercase")]
47pub enum State {
48 On,
49 Off,
50 Unknown,
52}
53
54#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Serialize)]
56#[serde(rename_all = "lowercase")]
57pub enum Severity {
58 #[default]
59 Info,
60 Notice,
61 Warning,
62}
63
64#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
66pub struct Item {
67 pub kind: Kind,
68 pub state: State,
69 #[serde(skip_serializing_if = "Option::is_none")]
71 pub count: Option<usize>,
72 pub severity: Severity,
73 #[serde(skip_serializing_if = "Vec::is_empty")]
75 pub hints: Vec<String>,
76}
77
78impl Item {
79 fn new(kind: Kind, state: State) -> Self {
80 Self {
81 kind,
82 state,
83 count: None,
84 severity: Severity::Info,
85 hints: Vec::new(),
86 }
87 }
88
89 fn switch(kind: Kind, on: Option<bool>) -> Self {
91 let state = match on {
92 Some(true) => State::On,
93 Some(false) => State::Off,
94 None => State::Unknown,
95 };
96 Self::new(kind, state)
97 }
98
99 fn list(kind: Kind, count: Option<usize>) -> Self {
101 Self {
102 count,
103 ..Self::switch(kind, count.map(|n| n > 0))
104 }
105 }
106
107 fn flag(mut self, when: bool, severity: Severity, hint: &str) -> Self {
109 if when {
110 self.severity = self.severity.max(severity);
111 self.hints.push(hint.to_owned());
112 }
113 self
114 }
115
116 fn when_on(self, severity: Severity, hint: &str) -> Self {
118 let on = self.state == State::On;
119 self.flag(on, severity, hint)
120 }
121
122 fn is_on(&self) -> bool {
123 self.state == State::On
124 }
125}
126
127pub(super) fn items(exposure: &Exposure, web: bool) -> Vec<Item> {
130 let without_web = |item: Item| {
131 let unknown = !web && item.state == State::Unknown;
132 item.flag(
133 unknown,
134 Severity::Info,
135 "needs a login to the web interface",
136 )
137 };
138 let forwardings = exposure
139 .port_forwardings
140 .as_ref()
141 .map(|all| all.iter().filter(|f| f.enabled).count());
142 vec![
143 remote_access(exposure.remote_access.as_ref()),
144 myfritz(exposure.myfritz.as_ref()),
145 Item::switch(
146 Kind::DynamicDns,
147 exposure.dynamic_dns.as_ref().map(|d| d.enabled),
148 ),
149 Item::list(Kind::PortForwarding, forwardings)
150 .when_on(Severity::Notice, "ports open to hosts of the home network"),
151 without_web(Item::switch(Kind::ExposedHost, exposure.exposed_host))
152 .when_on(Severity::Warning, "all ports of one host are open"),
153 without_web(Item::switch(Kind::Upnp, exposure.upnp))
154 .when_on(Severity::Notice, "hosts may open ports themselves"),
155 ftp(exposure.ftp.as_ref()),
156 shared_links(exposure),
157 without_web(Item::list(Kind::Vpn, exposure.vpn.as_ref().map(Vec::len))),
158 Item::switch(Kind::Tr069, exposure.tr069.as_ref().map(|t| t.enabled))
159 .when_on(Severity::Info, "the provider may manage the device"),
160 usp(exposure.usp.as_ref()),
161 ]
162}
163
164fn remote_access(access: Option<&RemoteAccess>) -> Item {
165 let item = Item::switch(Kind::RemoteAccess, access.map(|a| a.enabled));
166 let self_signed = item.is_on() && access.and_then(|a| a.lets_encrypt) == Some(false);
167 item.when_on(
168 Severity::Notice,
169 "web interface reachable from the internet",
170 )
171 .flag(
172 self_signed,
173 Severity::Notice,
174 "certificate signed by the device itself",
175 )
176}
177
178fn myfritz(myfritz: Option<&MyFritz>) -> Item {
179 let mut item = Item::switch(Kind::Myfritz, myfritz.map(|m| m.enabled));
180 if let Some(myfritz) = myfritz.filter(|_| item.is_on()) {
181 let shared = myfritz.services.iter().filter(|s| s.enabled).count();
182 item.count = Some(shared);
183 item = item.flag(
184 shared > 0,
185 Severity::Notice,
186 "services of the home network shared",
187 );
188 }
189 item
190}
191
192fn ftp(ftp: Option<&Ftp>) -> Item {
193 let item = Item::switch(Kind::Ftp, ftp.map(|f| f.enabled));
194 let plain = item.is_on() && ftp.and_then(|f| f.tls_only) != Some(true);
195 item.when_on(Severity::Notice, "storage reachable from the internet")
196 .flag(plain, Severity::Warning, "FTP without TLS allowed")
197}
198
199fn shared_links(exposure: &Exposure) -> Item {
200 let valid: Option<Vec<_>> = exposure
201 .shared_links
202 .as_ref()
203 .map(|links| links.iter().filter(|l| l.valid).collect());
204 let forever = valid
205 .iter()
206 .flatten()
207 .any(|link| link.expires.is_none() && link.access_limit.is_none());
208 Item::list(Kind::SharedLinks, valid.as_ref().map(Vec::len))
209 .when_on(Severity::Notice, "files of the storage shared")
210 .flag(forever, Severity::Notice, "links without expiry")
211}
212
213fn usp(usp: Option<&Usp>) -> Item {
214 let enabled: Option<Vec<_>> = usp.map(|u| u.controllers.iter().filter(|c| c.enabled).collect());
215 let mut item = Item::list(Kind::Usp, enabled.as_ref().map(Vec::len))
216 .when_on(Severity::Notice, "controllers may manage the device");
217 let plain = enabled.iter().flatten().any(|c| c.tls == Some(false));
218 if usp.and_then(|u| u.myfritz) == Some(true) {
219 item.state = State::On;
220 item = item.flag(true, Severity::Notice, "MyFRITZ may manage the device");
221 }
222 item.flag(plain, Severity::Warning, "controller without TLS")
223}
224
225#[cfg(test)]
226mod tests {
227 use super::super::{Controller, DynamicDns, Link, Service, Tr069, Vpn};
228 use super::*;
229 use crate::ports::Forwarding;
230
231 fn summary(items: &[Item]) -> Vec<(Kind, State, Option<usize>, Severity)> {
232 items
233 .iter()
234 .map(|i| (i.kind, i.state, i.count, i.severity))
235 .collect()
236 }
237
238 #[test]
239 fn everything_off_is_info() {
240 let exposure = Exposure {
241 remote_access: Some(RemoteAccess::default()),
242 myfritz: Some(MyFritz::default()),
243 dynamic_dns: Some(DynamicDns::default()),
244 port_forwardings: Some(vec![Forwarding::default()]),
245 exposed_host: Some(false),
246 upnp: Some(false),
247 ftp: Some(Ftp::default()),
248 shared_links: Some(vec![Link::default()]),
249 vpn: Some(Vec::new()),
250 tr069: Some(Tr069::default()),
251 usp: Some(Usp::default()),
252 ..Exposure::default()
253 };
254 let items = items(&exposure, true);
255 assert_eq!(items.len(), 11);
256 for item in &items {
257 assert_eq!(item.state, State::Off, "{item:?}");
258 assert_eq!(item.severity, Severity::Info, "{item:?}");
259 assert!(item.hints.is_empty(), "{item:?}");
260 }
261 }
262
263 #[test]
264 fn missing_parts_are_unknown() {
265 let items = items(&Exposure::default(), false);
266 for item in &items {
267 assert_eq!(item.state, State::Unknown, "{item:?}");
268 assert_eq!(item.severity, Severity::Info, "{item:?}");
269 }
270 let web: Vec<Kind> = items
271 .iter()
272 .filter(|i| !i.hints.is_empty())
273 .map(|i| i.kind)
274 .collect();
275 assert_eq!(web, [Kind::ExposedHost, Kind::Upnp, Kind::Vpn]);
276 assert_eq!(items[4].hints, ["needs a login to the web interface"]);
277 }
278
279 #[test]
280 fn rates_everything_on() {
281 use Severity::{Info, Notice, Warning};
282 use State::On;
283 let on = Exposure {
284 remote_access: Some(RemoteAccess {
285 enabled: true,
286 port: Some(443),
287 lets_encrypt: Some(false),
288 }),
289 myfritz: Some(MyFritz {
290 enabled: true,
291 services: vec![Service {
292 enabled: true,
293 ..Service::default()
294 }],
295 ..MyFritz::default()
296 }),
297 dynamic_dns: Some(DynamicDns {
298 enabled: true,
299 ..DynamicDns::default()
300 }),
301 port_forwardings: Some(vec![
302 Forwarding {
303 enabled: true,
304 ..Forwarding::default()
305 },
306 Forwarding::default(),
307 ]),
308 exposed_host: Some(true),
309 upnp: Some(true),
310 ftp: Some(Ftp {
311 enabled: true,
312 port: None,
313 tls_only: Some(false),
314 }),
315 shared_links: Some(vec![Link {
316 valid: true,
317 ..Link::default()
318 }]),
319 vpn: Some(vec![Vpn::default(), Vpn::default()]),
320 tr069: Some(Tr069 {
321 enabled: true,
322 ..Tr069::default()
323 }),
324 usp: Some(Usp {
325 myfritz: Some(true),
326 controllers: vec![Controller {
327 enabled: true,
328 tls: Some(false),
329 ..Controller::default()
330 }],
331 }),
332 ..Exposure::default()
333 };
334 let items = items(&on, true);
335 assert_eq!(
336 summary(&items),
337 [
338 (Kind::RemoteAccess, On, None, Notice),
339 (Kind::Myfritz, On, Some(1), Notice),
340 (Kind::DynamicDns, On, None, Info),
341 (Kind::PortForwarding, On, Some(1), Notice),
342 (Kind::ExposedHost, On, None, Warning),
343 (Kind::Upnp, On, None, Notice),
344 (Kind::Ftp, On, None, Warning),
345 (Kind::SharedLinks, On, Some(1), Notice),
346 (Kind::Vpn, On, Some(2), Info),
347 (Kind::Tr069, On, None, Info),
348 (Kind::Usp, On, Some(1), Warning),
349 ]
350 );
351 assert_eq!(
352 items[0].hints,
353 [
354 "web interface reachable from the internet",
355 "certificate signed by the device itself"
356 ]
357 );
358 assert_eq!(
359 items[7].hints,
360 ["files of the storage shared", "links without expiry"]
361 );
362 assert_eq!(
363 items[10].hints,
364 [
365 "controllers may manage the device",
366 "MyFRITZ may manage the device",
367 "controller without TLS"
368 ]
369 );
370 let exposure = Exposure { items, ..on };
371 assert!(exposure.has_warning());
372 assert!(!Exposure::default().has_warning());
373 }
374
375 #[test]
376 fn rates_safer_settings_lower() {
377 let exposure = Exposure {
378 remote_access: Some(RemoteAccess {
379 enabled: true,
380 port: None,
381 lets_encrypt: Some(true),
382 }),
383 myfritz: Some(MyFritz {
384 enabled: true,
385 ..MyFritz::default()
386 }),
387 ftp: Some(Ftp {
388 enabled: true,
389 port: None,
390 tls_only: Some(true),
391 }),
392 shared_links: Some(vec![Link {
393 valid: true,
394 expires: Some("2026-12-01T00:00:00".to_owned()),
395 ..Link::default()
396 }]),
397 usp: Some(Usp {
398 myfritz: Some(false),
399 controllers: vec![Controller {
400 enabled: true,
401 tls: Some(true),
402 ..Controller::default()
403 }],
404 }),
405 ..Exposure::default()
406 };
407 let items = items(&exposure, true);
408 assert_eq!(
409 items[0].hints,
410 ["web interface reachable from the internet"]
411 );
412 assert_eq!(
413 (items[1].state, items[1].count, items[1].severity),
414 (State::On, Some(0), Severity::Info)
415 );
416 assert_eq!(items[6].severity, Severity::Notice);
417 assert_eq!(items[7].hints, ["files of the storage shared"]);
418 assert_eq!(items[10].severity, Severity::Notice);
419 }
420}