summaryrefslogtreecommitdiff
path: root/crates/tor-config-path/src/addr.rs
blob: 97fb20f8d2b4cd43bb27e9eea3be5235f00e2a6e (plain)
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
//! Support for shell expansion in [`general::SocketAddr`].

use crate::{CfgPath, CfgPathError};
use serde::{Deserialize, Serialize};
use std::{io, net, path::PathBuf, str::FromStr, sync::Arc};
use tor_general_addr::{general, unix};

/// A variation of [`general::SocketAddr`] that allows shell expansions in Unix paths.
///
/// The string representation for these addresses is the same as for [`general::SocketAddr`];
/// but the shell expansion syntax is the same as for [`CfgPath`].
///
/// Shell expansion is only supported _within_ paths: Even if the user has set `${HOME}`
/// to `127.0.0.1`, the address `inet:${HOME}:9999` is a syntax error.
///
/// In addition to the "inet:" and "unix:" schemas supported by `general::SocketAddr`,
/// This type also supports a "unix-literal" schema,
/// to indicate that no shell expansion should occur.
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(into = "CfgAddrSerde", try_from = "CfgAddrSerde")]
pub struct CfgAddr(AddrInner);

/// Implementation type for `CfgAddr`.
///
/// This is a separate type because we can't define an public enum with private members.
#[derive(Clone, Debug, Eq, PartialEq)]
enum AddrInner {
    /// An internet address (which will not be expanded).
    Inet(net::SocketAddr),
    /// A unix domain socket path.
    Unix(CfgPath),
}

impl CfgAddr {
    /// Create a new [`CfgAddr`] that will produce an `AF_UNIX` address
    /// corresponding to the provided path.
    ///
    /// Note that not all platforms support AF\_UNIX addresses;
    /// on Windows, notably, expanding this path will produce an error.
    pub fn new_unix(path: CfgPath) -> Self {
        CfgAddr(AddrInner::Unix(path))
    }

    /// Return the [`general::SocketAddr`] produced by expanding this `CfgAddr`.
    #[cfg_attr(not(unix), expect(unused_variables))]
    pub fn address(
        &self,
        path_resolver: &crate::CfgPathResolver,
    ) -> Result<general::SocketAddr, CfgAddrError> {
        match &self.0 {
            AddrInner::Inet(socket_addr) => {
                // Easy case: This is an inet address.
                Ok((*socket_addr).into())
            }
            AddrInner::Unix(cfg_path) => {
                #[cfg(not(unix))]
                {
                    // Give this error early on non-unix platforms, so that we don't confuse the user.
                    return Err(unix::NoAfUnixSocketSupport::default().into());
                }
                #[cfg(unix)]
                {
                    let addr = unix::SocketAddr::from_pathname(cfg_path.path(path_resolver)?)
                        .map_err(|e| CfgAddrError::ConstructAfUnixAddress(Arc::new(e)))?;
                    Ok(addr.into())
                }
            }
        }
    }

    /// Return true if this address is of a type to which variable substitutions will apply.
    ///
    /// Currently, substitutions apply to AF\_UNIX addresses but not to Inet addresses.
    pub fn substitutions_will_apply(&self) -> bool {
        match &self.0 {
            AddrInner::Inet(_) => false,
            AddrInner::Unix(_) => true,
        }
    }

    /// Helper: if possible, format this address as a String.
    ///
    /// (This will return Err(p) if this path is a literal unix domain socket path
    /// that can't be represented as a string.)
    //
    // This is a separate function so that it can form the basis of a "display_lossy"
    // implementation, assuming we need one.
    fn try_to_string(&self) -> Result<String, &PathBuf> {
        use crate::PathInner as PI;
        use AddrInner as AI;
        match &self.0 {
            AI::Inet(socket_addr) => Ok(format!("inet:{}", socket_addr)),
            AI::Unix(cfg_path) => match &cfg_path.0 {
                PI::Shell(s) => Ok(format!("unix:{}", s)),
                PI::Literal(path) => match path.literal.to_str() {
                    Some(literal_as_str) => Ok(format!("unix-literal:{}", literal_as_str)),
                    None => Err(&path.literal),
                },
            },
        }
    }
}

/// Error produced when trying to expand a [`CfgAddr`] into a [`general::SocketAddr`].
#[derive(Clone, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum CfgAddrError {
    /// Tried to expand a `unix:` address on a platform where we don't support `AF_UNIX` addresses.
    #[error("No support for AF_UNIX addresses on this platform")]
    NoAfUnixSocketSupport(#[from] unix::NoAfUnixSocketSupport),
    /// Unable to expand the underlying `CfgPath`, likely due to syntax or missing variables.
    #[error("Could not expand path")]
    Path(#[from] CfgPathError),
    /// Unable to create an AF_UNIX address from a path.
    ///
    /// (This can happen if the path is too long, or contains internal NULs.)
    #[error("Could not construct AF_UNIX address")]
    ConstructAfUnixAddress(#[source] Arc<io::Error>),
}

impl FromStr for CfgAddr {
    type Err = general::AddrParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // NOTE: This logic is mostly duplicated from <FromStr for general::SocketAddr>;
        // I don't see an easy way to deduplicate it.
        if s.starts_with(|c: char| c.is_ascii_digit() || c == '[') {
            // This looks like an inet address, and cannot be a qualified address.
            Ok(s.parse::<net::SocketAddr>()?.into())
        } else if let Some((schema, remainder)) = s.split_once(':') {
            match schema {
                "unix" => {
                    let path = CfgPath::new(remainder.to_string());
                    Ok(CfgAddr::new_unix(path))
                }
                "unix-literal" => {
                    let path = CfgPath::new_literal(remainder.to_string());
                    Ok(CfgAddr::new_unix(path))
                }
                "inet" => Ok(remainder.parse::<net::SocketAddr>()?.into()),
                _ => Err(general::AddrParseError::UnrecognizedSchema(
                    schema.to_string(),
                )),
            }
        } else {
            Err(general::AddrParseError::NoSchema)
        }
    }
}

impl From<net::SocketAddr> for CfgAddr {
    fn from(value: net::SocketAddr) -> Self {
        CfgAddr(AddrInner::Inet(value))
    }
}
impl TryFrom<unix::SocketAddr> for CfgAddr {
    type Error = UnixAddrNotAPath;

    fn try_from(value: unix::SocketAddr) -> Result<Self, Self::Error> {
        // We don't need to check `#[cfg(unix)]` here:
        // if unix::SocketAddr is inhabited, then we can construct the Unix variant.
        Ok(Self::new_unix(CfgPath::new_literal(
            value.as_pathname().ok_or(UnixAddrNotAPath)?,
        )))
    }
}
// NOTE that we deliberately _don't_ implement From<Path> or From<CfgPath>;
// we want to keep open the possibility that there may be non-AF\_UNIX path-based
// addresses in the future!

/// Error returned when trying to convert a non-path `unix::SocketAddr` into a `CfgAddr` .
#[derive(Clone, Debug, Default, thiserror::Error)]
#[non_exhaustive]
#[error("Unix domain socket address was not a path.")]
pub struct UnixAddrNotAPath;

/// Serde helper: We convert CfgAddr through this format in order to serialize and deserialize it.
#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum CfgAddrSerde {
    /// We serialize most types as a string.
    Str(String),
    /// We have another format for representing AF\_UNIX address literals
    /// that can't be represented as a string.
    UnixLiteral {
        /// A path that won't be expanded.
        unix_literal: PathBuf,
    },
}

impl TryFrom<CfgAddrSerde> for CfgAddr {
    type Error = general::AddrParseError;

    fn try_from(value: CfgAddrSerde) -> Result<Self, Self::Error> {
        use CfgAddrSerde as S;
        match value {
            S::Str(s) => s.parse(),
            S::UnixLiteral { unix_literal } => {
                Ok(CfgAddr::new_unix(CfgPath::new_literal(unix_literal)))
            }
        }
    }
}
impl From<CfgAddr> for CfgAddrSerde {
    fn from(value: CfgAddr) -> Self {
        match value.try_to_string() {
            Ok(s) => CfgAddrSerde::Str(s),
            Err(unix_literal) => CfgAddrSerde::UnixLiteral {
                unix_literal: unix_literal.clone(),
            },
        }
    }
}

#[cfg(test)]
mod test {
    // @@ begin test lint list maintained by maint/add_warning @@
    #![allow(clippy::bool_assert_comparison)]
    #![allow(clippy::clone_on_copy)]
    #![allow(clippy::dbg_macro)]
    #![allow(clippy::mixed_attributes_style)]
    #![allow(clippy::print_stderr)]
    #![allow(clippy::print_stdout)]
    #![allow(clippy::single_char_pattern)]
    #![allow(clippy::unwrap_used)]
    #![allow(clippy::unchecked_time_subtraction)]
    #![allow(clippy::useless_vec)]
    #![allow(clippy::needless_pass_by_value)]
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->

    use super::*;
    use assert_matches::assert_matches;
    use std::path::PathBuf;

    use crate::{CfgPathResolver, home};

    #[test]
    fn parse_inet_ok() {
        fn check(s: &str) {
            let resolv = CfgPathResolver::from_pairs([("FOO", "foo")]);
            let a: general::SocketAddr = CfgAddr::from_str(s).unwrap().address(&resolv).unwrap();
            assert_eq!(a, general::SocketAddr::from_str(s).unwrap());
        }

        check("127.0.0.1:9999");
        check("inet:127.0.0.1:9999");
        check("[2001:db8::413]:443");
        check("inet:[2001:db8::413]:443");
    }

    #[test]
    fn parse_inet_bad() {
        assert_matches!(
            CfgAddr::from_str("612"),
            Err(general::AddrParseError::InvalidInetAddress(_))
        );
        assert_matches!(
            CfgAddr::from_str("612unix:/home"),
            Err(general::AddrParseError::InvalidInetAddress(_))
        );
        assert_matches!(
            CfgAddr::from_str("127.0.0.1.1:99"),
            Err(general::AddrParseError::InvalidInetAddress(_))
        );
        assert_matches!(
            CfgAddr::from_str("inet:6"),
            Err(general::AddrParseError::InvalidInetAddress(_))
        );
        assert_matches!(
            CfgAddr::from_str("[[[[[]]]]]"),
            Err(general::AddrParseError::InvalidInetAddress(_))
        );
    }

    #[test]
    fn parse_bad_schemas() {
        assert_matches!(
            CfgAddr::from_str("uranian:umbra"),
            Err(general::AddrParseError::UnrecognizedSchema(_))
        );
    }

    #[test]
    #[cfg_attr(not(unix), expect(unused_variables))]
    fn unix_literal() {
        let resolv = CfgPathResolver::from_pairs([("USER_HOME", home().unwrap())]);
        let pb = PathBuf::from("${USER_HOME}/.local/socket");
        let a1 = CfgAddr::new_unix(CfgPath::new_literal(&pb));
        let a2 = CfgAddr::from_str("unix-literal:${USER_HOME}/.local/socket").unwrap();
        #[cfg(unix)]
        {
            assert_eq!(a1.address(&resolv).unwrap(), a2.address(&resolv).unwrap(),);
            match a1.address(&resolv).unwrap() {
                general::SocketAddr::Unix(socket_addr) => {
                    // can't use assert_eq because these types are not Debug.
                    assert!(socket_addr.as_pathname() == Some(pb.as_ref()));
                }
                _ => panic!("Expected a unix domain socket address"),
            }
        }
        #[cfg(not(unix))]
        assert_matches!(
            a1.address(&resolv),
            Err(CfgAddrError::NoAfUnixSocketSupport(_))
        );
    }

    #[cfg_attr(not(unix), expect(unused_variables))]
    fn try_unix(addr: &str, want: &str, path_resolver: &CfgPathResolver) {
        let p = CfgPath::new(want.to_string());
        let expansion = p.path(path_resolver).unwrap();
        let cfg_addr = CfgAddr::from_str(addr).unwrap();
        assert_matches!(&cfg_addr.0, AddrInner::Unix(_));
        #[cfg(unix)]
        {
            let gen_addr = cfg_addr.address(path_resolver).unwrap();
            let expected_addr = unix::SocketAddr::from_pathname(expansion).unwrap();
            assert_eq!(gen_addr, expected_addr.into());
        }
        #[cfg(not(unix))]
        {
            assert_matches!(
                cfg_addr.address(path_resolver),
                Err(CfgAddrError::NoAfUnixSocketSupport(_))
            );
        }
    }

    #[test]
    fn unix_no_substitution() {
        let resolver = CfgPathResolver::from_pairs([("FOO", "foo")]);
        try_unix("unix:/home/mayor/.socket", "/home/mayor/.socket", &resolver);
    }

    #[test]
    #[cfg(feature = "expand-paths")]
    fn unix_substitution() {
        let resolver = CfgPathResolver::from_pairs([("FOO", "foo")]);
        try_unix("unix:${FOO}/socket", "${FOO}/socket", &resolver);
    }

    #[test]
    fn serde() {
        fn testcase_with_provided_addr(json: &str, addr: &CfgAddr) {
            let a1: CfgAddr = serde_json::from_str(json).unwrap();
            assert_eq!(&a1, addr);
            let encoded = serde_json::to_string(&a1).unwrap();
            let a2: CfgAddr = serde_json::from_str(&encoded).unwrap();
            assert_eq!(&a2, addr);
        }
        fn testcase(json: &str, addr: &str) {
            let addr = CfgAddr::from_str(addr).unwrap();
            testcase_with_provided_addr(json, &addr);
        }

        testcase(r#" "inet:127.0.0.1:443" "#, "inet:127.0.0.1:443");
        testcase(r#" "unix:${HOME}/socket" "#, "unix:${HOME}/socket");
        testcase(
            r#" "unix-literal:${HOME}/socket" "#,
            "unix-literal:${HOME}/socket",
        );
    }
}