Debian Codegraph @

src/commands/pki/path.rs

raw
1 use sequoia_openpgp as openpgp;
2 use openpgp::Result;
3
4 use sequoia_wot as wot;
5
6 use crate::Sq;
7 use crate::cli::pki::path::Command;
8 use crate::common::pki::output::print_path;
9 use crate::common::pki::output::print_path_error;
10 use crate::common::pki::output::print_path_header;
11 use crate::common::pki::required_trust_amount;
12 use crate::sq::TrustThreshold;
13
14 pub fn path(sq: Sq, c: Command)
15 -> Result<()>
16 {
17 let Command {
18 certification_network, trust_amount, path, userids,
19 } = c;
20
21 assert_eq!(userids.len(), 1, "guaranteed by clap");
22
23 let target = path.last().expect("guaranteed by clap");
24 let mut userid = None;
25 // The trust threshold parameter is irrelevant, because we're only
26 // considering fingerprints, and key IDs.
27 if let Ok((cert, _cert_handle))
28 = sq.resolve_cert(&target.into(), TrustThreshold::Full)
29 {
30 if let Ok(vc) = cert.with_policy(sq.policy, sq.time) {
31 if let Ok(userids) = userids.resolve(&vc) {
32 assert_eq!(userids.len(), 1);
33 userid = Some(userids.into_iter().next().unwrap());
34 }
35 }
36 }
37 let userid = userid.unwrap_or_else(|| {
38 userids.iter().next().unwrap().resolve_to_self()
39 });
40
41 // Build the network.
42 let cert_store = sq.cert_store_or_else()?;
43 let mut n = wot::NetworkBuilder::rooted(cert_store, &*sq.trust_roots());
44 if *certification_network {
45 n = n.certification_network();
46 }
47 let q = n.build();
48
49 let required_amount =
50 required_trust_amount(*trust_amount, *certification_network)?;
51
52 assert!(path.len() > 0, "guaranteed by clap");
53
54 let o = &mut std::io::stdout();
55
56 let r = q.lint_path(&path, userid.userid(), required_amount, sq.policy);
57
58 let target_kh = path.last().expect("have one");
59
60 let trust_amount = match r {
61 Ok(path) => {
62 print_path_header(
63 o,
64 target_kh,
65 userid.userid(),
66 path.amount(),
67 required_amount,
68 );
69 print_path(o, &path, userid.userid(), " ")?;
70
71 let trust_amount = path.amount();
72 if trust_amount >= required_amount {
73 return Ok(());
74 }
75
76 trust_amount
77 }
78 Err(err) => {
79 print_path_header(
80 o,
81 target_kh,
82 userid.userid(),
83 0,
84 required_amount,
85 );
86
87 print_path_error(o, err);
88
89 0
90 }
91 };
92
93 Err(anyhow::anyhow!(
94 "The path is not sufficient to authenticate the binding. \
95 Its trust amount is {}, but a trust amount of {} is required.",
96 trust_amount, required_amount))
97 }