fritz-core 0.2.2

FRITZ!Box specific logic for the fritz CLI and MCP server (unofficial, not affiliated with FRITZ! GmbH)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! What `fritz capture` reads from a device, and what it leaves alone.
//!
//! The capture only ever calls getters. Of those it skips the ones that need
//! input other than an index, the ones that could return secrets, and the
//! ones that return personal lists unless asked for. The decision depends
//! only on the SCPD, so the plan can be reviewed before anything is called.

use std::fmt;

use tr064::{Action, Client, DeviceDescription, Direction, Scpd, Service};

use crate::policy::{self, Class};

mod run;

pub use run::{Capture, List, Options, Outcome, Response, run, write};

/// What the capture does with one action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Decision {
    /// A getter without input: called once.
    Call,
    /// A getter whose only input is an index: called for each entry.
    Iterate { index: String },
    /// A getter that needs other input: not called.
    NeedsInput { inputs: Vec<String> },
    /// A getter for personal lists like calls or contacts: only called when
    /// asked for.
    Personal,
    /// A getter that could return secrets: never called.
    Denied { reason: String },
    /// Not a getter: never called.
    NotGetter,
}

/// Decides what the capture does with an action of a service, following
/// its [class](policy::classify).
pub fn decide(service: &Service, action: &Action) -> Decision {
    match policy::classify(service, action) {
        Class::Read => shape(action),
        Class::Personal => Decision::Personal,
        Class::Secret { reason } => Decision::Denied { reason },
        Class::Write | Class::Critical { .. } => Decision::NotGetter,
    }
}

/// How a getter is called, depending on its inputs: [`Decision::Call`],
/// [`Decision::Iterate`] or [`Decision::NeedsInput`].
fn shape(action: &Action) -> Decision {
    let inputs: Vec<String> = action
        .arguments
        .iter()
        .filter(|a| a.direction == Direction::In)
        .map(|a| a.name.clone())
        .collect();
    match inputs.as_slice() {
        [] => Decision::Call,
        [index] if index.ends_with("Index") => Decision::Iterate {
            index: index.clone(),
        },
        _ => Decision::NeedsInput { inputs },
    }
}

/// The decisions for all actions of a device, with the documents they are
/// based on.
#[derive(Debug)]
pub struct Plan {
    /// The device description as the device sent it.
    pub description: String,
    pub services: Vec<ServicePlan>,
}

/// The decisions for the actions of one service, or why its SCPD could not
/// be read.
#[derive(Debug)]
pub struct ServicePlan {
    pub service: Service,
    pub scpd: Result<ServiceScpd, tr064::Error>,
}

/// The SCPD of a service and the decision for each of its actions.
#[derive(Debug)]
pub struct ServiceScpd {
    /// The SCPD as the device sent it.
    pub xml: String,
    pub actions: Vec<(Action, Decision)>,
}

/// Reads the device description and all SCPDs and decides for each action.
/// Calls no action. A service whose SCPD cannot be read is part of the plan
/// with its error.
///
/// # Errors
///
/// The errors of reading and parsing the device description.
pub async fn plan(client: &Client) -> Result<Plan, tr064::Error> {
    let description = client.get("/tr64desc.xml").await?;
    let parsed = DeviceDescription::parse(&description)?;
    let mut services = Vec::new();
    for service in parsed.services() {
        services.push(ServicePlan {
            service: service.clone(),
            scpd: plan_service(client, service).await,
        });
    }
    Ok(Plan {
        description,
        services,
    })
}

async fn plan_service(client: &Client, service: &Service) -> Result<ServiceScpd, tr064::Error> {
    let xml = client.get(&service.scpd_url).await?;
    let actions = Scpd::parse(&xml)?
        .actions
        .into_iter()
        .map(|action| {
            let decision = decide(service, &action);
            (action, decision)
        })
        .collect();
    Ok(ServiceScpd { xml, actions })
}

/// The last part of the service ID, like `Hosts1`. Unique within a device.
pub fn short_id(service: &Service) -> &str {
    service.service_id.rsplit(':').next().unwrap_or_default()
}

/// How many actions got each decision, and how many services failed.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Summary {
    pub call: usize,
    pub iterate: usize,
    pub needs_input: usize,
    pub personal: usize,
    pub denied: usize,
    pub not_getter: usize,
    pub failed_services: usize,
}

impl Summary {
    fn count(&mut self, decision: &Decision) {
        let counter = match decision {
            Decision::Call => &mut self.call,
            Decision::Iterate { .. } => &mut self.iterate,
            Decision::NeedsInput { .. } => &mut self.needs_input,
            Decision::Personal => &mut self.personal,
            Decision::Denied { .. } => &mut self.denied,
            Decision::NotGetter => &mut self.not_getter,
        };
        *counter += 1;
    }
}

impl Plan {
    /// Counts the decisions.
    pub fn summary(&self) -> Summary {
        let mut summary = Summary::default();
        for service in &self.services {
            match &service.scpd {
                Ok(scpd) => scpd.actions.iter().for_each(|(_, d)| summary.count(d)),
                Err(_) => summary.failed_services += 1,
            }
        }
        summary
    }
}

impl fmt::Display for Plan {
    /// One block per service listing the getters, followed by a summary.
    /// Actions that are not getters are only counted.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for service in &self.services {
            write_service(f, service)?;
        }
        let s = self.summary();
        write!(
            f,
            "{} services: {} to call, {} to iterate, {} need input, {} personal, \
             {} denied, {} not getters, {} SCPDs failed",
            self.services.len(),
            s.call,
            s.iterate,
            s.needs_input,
            s.personal,
            s.denied,
            s.not_getter,
            s.failed_services
        )
    }
}

fn write_service(f: &mut fmt::Formatter<'_>, plan: &ServicePlan) -> fmt::Result {
    let service = &plan.service;
    let id = short_id(service);
    writeln!(f, "{}:{} ({id})", service.name(), service.version())?;
    match &plan.scpd {
        Ok(scpd) => {
            let mut not_getters = 0;
            for (action, decision) in &scpd.actions {
                match decision {
                    Decision::NotGetter => not_getters += 1,
                    _ => writeln!(f, "  {}", Line(&action.name, decision))?,
                }
            }
            if not_getters > 0 {
                writeln!(f, "  never        not getters: {not_getters}")?;
            }
        }
        Err(error) => writeln!(f, "  SCPD failed: {error}")?,
    }
    writeln!(f)
}

/// One action of the plan, like `iterate      GetGenericHostEntry (NewIndex)`.
struct Line<'a>(&'a str, &'a Decision);

impl fmt::Display for Line<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Line(name, decision) = self;
        match decision {
            Decision::Call => write!(f, "call         {name}"),
            Decision::Iterate { index } => write!(f, "iterate      {name} ({index})"),
            Decision::NeedsInput { inputs } => {
                write!(f, "needs input  {name} ({})", inputs.join(", "))
            }
            Decision::Personal => write!(f, "personal     {name}"),
            Decision::Denied { reason } => write!(f, "denied       {name}: {reason}"),
            Decision::NotGetter => write!(f, "never        {name}"),
        }
    }
}

#[cfg(test)]
mod tests {
    use tr064::Argument;

    use super::*;

    fn service(name: &str) -> Service {
        Service {
            service_type: format!("urn:dslforum-org:service:{name}:1"),
            service_id: format!("urn:{name}-com:serviceId:{}1", name.to_lowercase()),
            control_url: "/upnp/control/x".to_owned(),
            event_sub_url: None,
            scpd_url: "/x.xml".to_owned(),
        }
    }

    fn action(name: &str, inputs: &[&str], outputs: &[&str]) -> Action {
        let argument = |direction| {
            move |name: &&str| Argument {
                name: (*name).to_owned(),
                direction,
                related_state_variable: "X".to_owned(),
            }
        };
        Action {
            name: name.to_owned(),
            arguments: inputs
                .iter()
                .map(argument(Direction::In))
                .chain(outputs.iter().map(argument(Direction::Out)))
                .collect(),
        }
    }

    fn decide_for(service_name: &str, a: &Action) -> Decision {
        decide(&service(service_name), a)
    }

    #[test]
    fn calls_getters_without_input() {
        let get_info = action("GetInfo", &[], &["NewSerialNumber"]);
        assert_eq!(decide_for("DeviceInfo", &get_info), Decision::Call);
        let avm = action("X_AVM-DE_GetNumberOfEntries", &[], &[]);
        assert_eq!(decide_for("Hosts", &avm), Decision::Call);
    }

    #[test]
    fn iterates_getters_with_only_an_index() {
        let entry = action("GetGenericHostEntry", &["NewIndex"], &["NewIPAddress"]);
        assert_eq!(
            decide_for("Hosts", &entry),
            Decision::Iterate {
                index: "NewIndex".to_owned()
            }
        );
    }

    #[test]
    fn skips_getters_that_need_other_input() {
        for inputs in [&["NewMACAddress"][..], &["NewIndex", "NewOther"]] {
            let a = action("GetSpecificHostEntry", inputs, &[]);
            assert_eq!(
                decide_for("Hosts", &a),
                Decision::NeedsInput {
                    inputs: inputs.iter().map(|s| (*s).to_owned()).collect()
                }
            );
        }
    }

    #[test]
    fn never_calls_other_actions() {
        for name in [
            "SetEnable",
            "Reboot",
            "X_AVM-DE_CreateUrlSID",
            "X_AVM-DE_SetGet",
        ] {
            let a = action(name, &[], &[]);
            assert_eq!(
                decide_for("DeviceConfig", &a),
                Decision::NotGetter,
                "{name}"
            );
        }
    }

    #[test]
    fn denies_getters_that_could_return_secrets() {
        let cases = [
            (
                "DeviceConfig",
                action("X_AVM-DE_GetConfigFile", &["NewX_AVM-DE_Password"], &[]),
            ),
            ("DeviceConfig", action("GetPersistentData", &[], &[])),
            (
                "X_VoIP",
                action("X_AVM-DE_GetClient3", &["NewX_AVM-DE_ClientIndex"], &[]),
            ),
            ("WLANConfiguration", action("GetSecurityKeys", &[], &[])),
            (
                "WLANConfiguration",
                action("GetInfo", &[], &["NewSSID", "NewWEPKey0"]),
            ),
            (
                "WLANConfiguration",
                action("X_AVM-DE_GetWPSInfo", &[], &["NewX_AVM-DE_WPSClientPIN"]),
            ),
            (
                "LANConfigSecurity",
                action("X_AVM-DE_GetCurrentUser", &[], &["NewX_AVM-DE_UserRights"]),
            ),
        ];
        for (service_name, a) in &cases[..6] {
            assert!(
                matches!(decide_for(service_name, a), Decision::Denied { .. }),
                "{}",
                a.name
            );
        }
        let (service_name, a) = &cases[6];
        assert_eq!(decide_for(service_name, a), Decision::Call);
        assert_eq!(
            decide_for("WLANConfiguration", &cases[4].1),
            Decision::Denied {
                reason: "returns NewWEPKey0, which points to a secret".to_owned()
            }
        );
    }

    #[test]
    fn calls_getters_that_only_describe_secrets() {
        let cases = [
            (
                "WLANConfiguration",
                action(
                    "GetInfo",
                    &[],
                    &["NewMinCharsPSK", "NewMaxCharsPSK", "NewAllowedCharsPSK"],
                ),
            ),
            (
                "WLANConfiguration",
                action("GetDefaultWEPKeyIndex", &[], &["NewDefaultWEPKeyIndex"]),
            ),
            (
                "UserInterface",
                action("GetInfo", &[], &["NewPasswordRequired"]),
            ),
            (
                "X_VoIP",
                action("GetInfoEx", &[], &["NewVoIPPasswordMinChars"]),
            ),
            (
                "X_AVM-DE_WANMobileConnection",
                action("GetInfo", &[], &["NewPINFailureCount"]),
            ),
            (
                "WANPPPConnection",
                action("GetInfo", &[], &["NewMaxCharsPassword", "NewUserName"]),
            ),
            (
                "ManagementServer",
                action("GetInfo", &[], &["NewUsername", "NewParameterKey"]),
            ),
        ];
        for (service_name, a) in &cases {
            assert_eq!(
                decide_for(service_name, a),
                Decision::Call,
                "{service_name} {}",
                a.name
            );
        }
    }

    #[test]
    fn marks_personal_lists() {
        for (service_name, name) in [
            ("X_AVM-DE_OnTel", "GetCallList"),
            ("X_AVM-DE_OnTel", "GetPhonebookList"),
            ("X_AVM-DE_TAM", "GetMessageList"),
            ("DeviceInfo", "GetDeviceLog"),
        ] {
            let a = action(name, &[], &[]);
            assert_eq!(decide_for(service_name, &a), Decision::Personal, "{name}");
        }
        let deflections = action("GetDeflections", &[], &[]);
        assert_eq!(decide_for("X_AVM-DE_OnTel", &deflections), Decision::Call);
    }

    #[test]
    fn renders_every_decision() {
        let lines = [
            (Decision::Call, "call         A"),
            (Decision::Personal, "personal     A"),
            (Decision::NotGetter, "never        A"),
        ];
        for (decision, expected) in lines {
            assert_eq!(Line("A", &decision).to_string(), expected);
        }
    }
}