tag2upload_service_manager/
dns.rs

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

use crate::prelude::*;

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct DnsGlobPattern(glob::Pattern);

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum AllowedCaller {
    Addr(IpNet),
    /// Names must not contain `:`
    /// and their last dotted component must contain some nondigits.
    Name(DnsGlobPattern),
}

pub struct IsAllowedCaller { _hidden: () }

#[derive(Error, Debug, Clone)]
#[error("syntactically invalid IP network or DNS name (or glob pattern)")]
pub struct InvalidAllowedCaller;

#[derive(Error, Debug, Clone)]
pub enum DisallowedCaller {
    Unresolved { addr: IpAddr },
    Resolved { addr: IpAddr, names: Vec<String> },
}

pub type Resolver = hickory_resolver::TokioAsyncResolver;

impl IsAllowedCaller {
    pub fn new_unchecked() -> Self {
        IsAllowedCaller { _hidden: () }
    }
}

impl Display for DisallowedCaller {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            DisallowedCaller::Unresolved { addr } =>
                write!(f, "unrecognised calling IP address {addr}")?,
            DisallowedCaller::Resolved { addr, names } => {
                write!(f, "unrecognised calling host [{addr}]")?;
                for n in names {
                    write!(f, "/{n:?}")?;
                }
            }
        }
        Ok(())
    }
}

impl FromStr for DnsGlobPattern {
    type Err = InvalidAllowedCaller;

    fn from_str(s: &str) -> Result<Self, InvalidAllowedCaller> {
        (|| {
            if s.contains(':') {
                return None;
            }
            if ! "*[".chars().any(|c| s.contains(c)) {
                // It's not yet definitely a glob pattern;
                // nor is it definitely an IPv6 address.
                // See if the last component is all digits - then
                // it's probably a (possibly invalid) IP address.
                let rhs = s.rsplit_once('.').map(|(_, r)| r).unwrap_or(s);
                if rhs.chars().all(|c| c.is_ascii_digit()) {
                    return None;
                }
            }
            let s = s.to_ascii_lowercase();
            let g = glob::Pattern::new(&s).ok()?;
            Some(DnsGlobPattern(g))
        })().ok_or(InvalidAllowedCaller)
    }
}

impl FromStr for AllowedCaller {
    type Err = InvalidAllowedCaller;
    fn from_str(s: &str) -> Result<Self, InvalidAllowedCaller> {
        Ok(if let Ok(net) = s.parse() {
            AllowedCaller::Addr(net)
        } else if let Ok(pat) = s.parse() {
            AllowedCaller::Name(pat)
        } else {
            return Err(InvalidAllowedCaller);
        })
    }
}

impl<'de> Deserialize<'de> for AllowedCaller {
    fn deserialize<D: Deserializer<'de>>(
        deser: D,
    ) -> Result<AllowedCaller, D::Error> {
        let s: String = String::deserialize(deser)?;
        s.parse()
            .map_err(|_| D::Error::invalid_value(
                serde::de::Unexpected::Str(&s),
                &"dns glob pattern or IP address mask",
            ))
    }
}

impl AllowedCaller {
    /// The `String` is the hostname, if it was found
    pub async fn list_contains(
        allowed: &[AllowedCaller],
        addr: IpAddr,
    ) -> Result<Result<IsAllowedCaller, DisallowedCaller>, AE> {
        let mut any_names = None;

        if let Some(y) = allowed.iter().find_map(|a| match a {
            AllowedCaller::Addr(a) => a.contains(&addr)
                .then(|| IsAllowedCaller::new_unchecked()),
            AllowedCaller::Name(_) => {
                any_names = Some(());
                None
            }
        }) {
            return Ok(Ok(y));
        }

        let Some(()) = any_names
        else { return Ok(Err(DisallowedCaller::Unresolved { addr })) };

        let names = globals().dns_resolver
            .reverse_lookup(addr).await
            .context("reverse lookup for {addr}")?
            .iter()
            .map(|ptr| ptr.to_lowercase().to_ascii())
            .collect_vec();

        if let Some(y) = allowed.iter().find_map(|a| {
            let AllowedCaller::Name(g) = a
            else { return None };

            names.iter().find_map(|n| {
                g.0.matches(n)
                    .then(|| IsAllowedCaller::new_unchecked())
            })
        }) {
            return Ok(Ok(y))
        }

        Ok(Err(DisallowedCaller::Resolved { addr, names }))
    }
}

#[test]
fn chk_allowed_caller() {
    let chk_name = |s: &str| {
        let allow: AllowedCaller = s.parse().expect(s);
        let glob = DnsGlobPattern(s.parse().expect(s));
        assert_eq!(allow, AllowedCaller::Name(glob));
    };
    let chk_addr = |s: &str| {
        let allow: AllowedCaller = s.parse().expect(s);
        let addr = s.parse().expect(s);
        assert_eq!(allow, AllowedCaller::Addr(addr));
    };
    let chk_err = |s: &str| {
        s.parse::<AllowedCaller>().err().expect(s);
    };
    chk_name("*");
    chk_name("a");
    chk_name("a.b");
    chk_name("3com");
    chk_name("a.3com");
    chk_name("[a]");

    chk_addr("::1/128");
    chk_addr("12.0.0.1/32");
    chk_addr("::/0");
    chk_addr("0.0.0.0/0");

    chk_err("1");
    chk_err("127.0.0.1");
    chk_err("::1");
}