1use serde::Deserialize;
4use serde_with::DeserializeFromStr;
5use std::{
6 fmt::Debug,
7 net::{self, IpAddr},
8 path::PathBuf,
9 str::FromStr,
10};
11use tor_config_path::{
12 CfgPath, CfgPathError, CfgPathResolver,
13 addr::{CfgAddr, CfgAddrError},
14};
15use tor_general_addr::general::{self, AddrParseError};
16#[cfg(feature = "rpc-server")]
17use tor_rtcompat::{NetStreamListener, NetStreamProvider};
18
19use crate::HasClientErrorAction;
20
21#[derive(Clone, Debug)]
31pub struct ParsedConnectPoint(ConnectPointEnum<Unresolved>);
32
33#[derive(Clone, Debug)]
41pub struct ResolvedConnectPoint(pub(crate) ConnectPointEnum<Resolved>);
42
43impl ParsedConnectPoint {
44 pub fn resolve(
47 &self,
48 resolver: &CfgPathResolver,
49 ) -> Result<ResolvedConnectPoint, ResolveError> {
50 use ConnectPointEnum as CPE;
51 Ok(ResolvedConnectPoint(match &self.0 {
52 CPE::Connect(connect) => CPE::Connect(connect.resolve(resolver)?),
53 CPE::Builtin(builtin) => CPE::Builtin(builtin.clone()),
54 }))
55 }
56
57 pub fn superuser_permission(&self) -> crate::SuperuserPermission {
59 self.0.superuser_permission()
60 }
61
62 pub fn is_explicit_abort(&self) -> bool {
64 self.0.is_explicit_abort()
65 }
66}
67
68impl ResolvedConnectPoint {
69 pub fn superuser_permission(&self) -> crate::SuperuserPermission {
71 self.0.superuser_permission()
72 }
73
74 pub fn is_explicit_abort(&self) -> bool {
76 self.0.is_explicit_abort()
77 }
78}
79
80impl FromStr for ParsedConnectPoint {
81 type Err = ParseError;
82
83 fn from_str(s: &str) -> Result<Self, Self::Err> {
84 let de: ConnectPointDe = toml::from_str(s).map_err(ParseError::InvalidConnectPoint)?;
85 Ok(ParsedConnectPoint(de.try_into()?))
86 }
87}
88
89#[derive(Clone, Debug, thiserror::Error)]
91#[non_exhaustive]
92pub enum ParseError {
93 #[error("Invalid connect point")]
95 InvalidConnectPoint(#[source] toml::de::Error),
96 #[error("Conflicting members in connect point")]
99 ConflictingMembers,
100 #[error("Unrecognized format on connect point")]
103 UnrecognizedFormat,
104 #[error("inet-auto address was not a loopback address")]
110 AutoAddressNotLoopback,
111}
112impl HasClientErrorAction for ParseError {
113 fn client_action(&self) -> crate::ClientErrorAction {
114 use crate::ClientErrorAction as A;
115 match self {
116 ParseError::InvalidConnectPoint(_) => A::Abort,
117 ParseError::ConflictingMembers => A::Abort,
118 ParseError::AutoAddressNotLoopback => A::Decline,
119 ParseError::UnrecognizedFormat => A::Decline,
120 }
121 }
122}
123
124#[derive(Clone, Debug, thiserror::Error)]
126#[non_exhaustive]
127pub enum ResolveError {
128 #[error("Unable to resolve variables in path")]
130 InvalidPath(#[from] CfgPathError),
131 #[error("Unable to parse address")]
133 UnparseableAddr(#[from] AddrParseError),
134 #[error("Unable to resolve variables in address")]
136 InvalidAddr(#[from] CfgAddrError),
137 #[error("Cannot represent expanded path as string")]
139 PathNotString,
140 #[error("Tried to bind or connect to a non-loopback TCP address")]
142 AddressNotLoopback,
143 #[error("Authorization type not compatible with address family")]
145 AuthNotCompatible,
146 #[error("Authorization type not recognized as a supported type")]
148 AuthNotRecognized,
149 #[error("Address type not recognized")]
153 AddressTypeNotRecognized,
154 #[error("inet-auto without socket_address_file, or vice versa")]
156 AutoIncompatibleWithSocketFile,
157 #[error("Path was not absolute")]
159 PathNotAbsolute,
160}
161impl HasClientErrorAction for ResolveError {
162 fn client_action(&self) -> crate::ClientErrorAction {
163 use crate::ClientErrorAction as A;
164 match self {
165 ResolveError::InvalidPath(e) => e.client_action(),
166 ResolveError::UnparseableAddr(e) => e.client_action(),
167 ResolveError::InvalidAddr(e) => e.client_action(),
168 ResolveError::PathNotString => A::Decline,
169 ResolveError::AddressNotLoopback => A::Decline,
170 ResolveError::AuthNotCompatible => A::Abort,
171 ResolveError::AuthNotRecognized => A::Decline,
172 ResolveError::AddressTypeNotRecognized => A::Decline,
173 ResolveError::PathNotAbsolute => A::Abort,
174 ResolveError::AutoIncompatibleWithSocketFile => A::Abort,
175 }
176 }
177}
178
179#[derive(Clone, Debug)]
185pub(crate) enum ConnectPointEnum<R: Addresses> {
186 Connect(Connect<R>),
188 Builtin(Builtin),
192}
193
194pub(crate) trait Addresses {
199 type SocketAddr: Clone + std::fmt::Debug;
201 type Path: Clone + std::fmt::Debug;
203}
204
205#[derive(Deserialize, Clone, Debug)]
215struct ConnectPointDe {
216 connect: Option<Connect<Unresolved>>,
218 builtin: Option<Builtin>,
220}
221impl TryFrom<ConnectPointDe> for ConnectPointEnum<Unresolved> {
222 type Error = ParseError;
223
224 fn try_from(value: ConnectPointDe) -> Result<Self, Self::Error> {
225 match value {
226 ConnectPointDe {
227 connect: Some(c),
228 builtin: None,
229 } => Ok(ConnectPointEnum::Connect(c)),
230 ConnectPointDe {
231 connect: None,
232 builtin: Some(b),
233 } => Ok(ConnectPointEnum::Builtin(b)),
234 ConnectPointDe {
235 connect: Some(_),
236 builtin: Some(_),
237 } => Err(ParseError::ConflictingMembers),
238 _ => Err(ParseError::UnrecognizedFormat),
241 }
242 }
243}
244
245impl<R: Addresses> ConnectPointEnum<R> {
246 fn superuser_permission(&self) -> crate::SuperuserPermission {
248 use crate::SuperuserPermission::*;
249 match self {
250 ConnectPointEnum::Connect(connect) => {
251 if connect.superuser {
252 Allowed
253 } else {
254 NotAllowed
255 }
256 }
257 ConnectPointEnum::Builtin(_) => NotAllowed,
258 }
259 }
260
261 fn is_explicit_abort(&self) -> bool {
263 matches!(
264 self,
265 ConnectPointEnum::Builtin(Builtin {
266 builtin: BuiltinVariant::Abort
267 })
268 )
269 }
270}
271
272#[derive(Deserialize, Clone, Debug)]
278pub(crate) struct Builtin {
279 pub(crate) builtin: BuiltinVariant,
281}
282
283#[derive(Deserialize, Clone, Debug)]
285#[serde(rename_all = "lowercase")]
286pub(crate) enum BuiltinVariant {
287 Abort,
290}
291
292#[derive(Deserialize, Clone, Debug)]
294#[serde(bound = "R::Path : Deserialize<'de>, AddrWithStr<R::SocketAddr> : Deserialize<'de>")]
295pub(crate) struct Connect<R: Addresses> {
296 pub(crate) socket: ConnectAddress<R>,
299 pub(crate) socket_canonical: Option<AddrWithStr<R::SocketAddr>>,
307 pub(crate) auth: Auth<R>,
310 pub(crate) socket_address_file: Option<R::Path>,
312 #[serde(default)]
315 pub(crate) superuser: bool,
316}
317
318#[derive(Deserialize, Clone, Debug)]
322#[serde(bound = "R::Path : Deserialize<'de>, AddrWithStr<R::SocketAddr> : Deserialize<'de>")]
323#[serde(untagged, expecting = "a network schema and address")]
324pub(crate) enum ConnectAddress<R: Addresses> {
325 InetAuto(InetAutoAddress),
327 Socket(AddrWithStr<R::SocketAddr>),
329}
330
331#[derive(Clone, Debug, DeserializeFromStr)]
333pub(crate) struct InetAutoAddress {
334 bind: Option<IpAddr>,
338}
339impl std::fmt::Display for InetAutoAddress {
340 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
341 match self.bind {
342 Some(a) => write!(f, "inet-auto:{a}"),
343 None => write!(f, "inet-auto:auto"),
344 }
345 }
346}
347impl FromStr for InetAutoAddress {
348 type Err = ParseError;
349
350 fn from_str(s: &str) -> Result<Self, Self::Err> {
351 let Some(addr_part) = s.strip_prefix("inet-auto:") else {
352 return Err(ParseError::UnrecognizedFormat);
353 };
354 if addr_part == "auto" {
355 return Ok(InetAutoAddress { bind: None });
356 }
357 let Ok(addr) = IpAddr::from_str(addr_part) else {
358 return Err(ParseError::UnrecognizedFormat);
359 };
360 if addr.is_loopback() {
361 Ok(InetAutoAddress { bind: Some(addr) })
362 } else {
363 Err(ParseError::AutoAddressNotLoopback)
364 }
365 }
366}
367
368impl InetAutoAddress {
369 fn bind_to_addresses(&self) -> Vec<general::SocketAddr> {
371 match self {
372 InetAutoAddress { bind: None } => vec![
373 net::SocketAddr::new(net::Ipv4Addr::LOCALHOST.into(), 0).into(),
374 net::SocketAddr::new(net::Ipv6Addr::LOCALHOST.into(), 0).into(),
375 ],
376 InetAutoAddress { bind: Some(ip) } => {
377 vec![net::SocketAddr::new(*ip, 0).into()]
378 }
379 }
380 }
381
382 #[cfg(feature = "rpc-client")]
386 pub(crate) fn validate_parsed_address(
387 &self,
388 addr: &general::SocketAddr,
389 ) -> Result<(), crate::ConnectError> {
390 use general::SocketAddr::Inet;
391 for sa in self.bind_to_addresses() {
392 if let (Inet(specified), Inet(got)) = (sa, addr) {
393 if specified.port() == 0 && specified.ip() == got.ip() {
394 return Ok(());
395 }
396 }
397 }
398
399 Err(crate::ConnectError::SocketAddressFileMismatch)
400 }
401}
402
403#[derive(Clone, Debug)]
405#[cfg_attr(feature = "rpc-client", derive(Deserialize))]
406#[cfg_attr(feature = "rpc-server", derive(serde::Serialize))]
407pub(crate) struct AddressFile {
408 pub(crate) address: String,
410}
411
412impl<R: Addresses> ConnectAddress<R> {
413 fn is_auto(&self) -> bool {
415 matches!(self, ConnectAddress::InetAuto { .. })
416 }
417}
418impl ConnectAddress<Unresolved> {
419 fn resolve(
421 &self,
422 resolver: &CfgPathResolver,
423 ) -> Result<ConnectAddress<Resolved>, ResolveError> {
424 use ConnectAddress::*;
425 match self {
426 InetAuto(a) => Ok(InetAuto(a.clone())),
427 Socket(s) => Ok(Socket(s.resolve(resolver)?)),
428 }
429 }
430}
431impl ConnectAddress<Resolved> {
432 fn bind_to_addresses(&self) -> Vec<general::SocketAddr> {
434 use ConnectAddress::*;
435 match self {
436 InetAuto(a) => a.bind_to_addresses(),
437 Socket(a) => vec![a.as_ref().clone()],
438 }
439 }
440
441 #[cfg(feature = "rpc-server")]
444 pub(crate) async fn bind<R>(
445 &self,
446 runtime: &R,
447 ) -> Result<(R::Listener, String), crate::ConnectError>
448 where
449 R: NetStreamProvider<general::SocketAddr>,
450 {
451 use crate::ConnectError;
452 match self {
453 ConnectAddress::InetAuto(auto) => {
454 let bind_one =
455 async |addr: &general::SocketAddr| -> Result<(R::Listener, String), crate::ConnectError> {
456 let listener = runtime.listen(addr).await?;
457 let local_addr = listener.local_addr()?.try_to_string().ok_or_else(|| ConnectError::Internal("Can't represent auto socket as string!".into()))?;
458 Ok((listener,local_addr))
459 };
460
461 let mut first_error = None;
462
463 for addr in auto.bind_to_addresses() {
464 match bind_one(&addr).await {
465 Ok(result) => {
466 return Ok(result);
467 }
468 Err(e) => {
469 if first_error.is_none() {
470 first_error = Some(e);
471 }
472 }
473 }
474 }
475 Err(first_error.unwrap_or_else(|| {
477 ConnectError::Internal("No auto addresses to bind!?".into())
478 }))
479 }
480 ConnectAddress::Socket(addr) => {
481 let listener = runtime.listen(addr.as_ref()).await?;
482 Ok((listener, addr.as_str().to_owned()))
483 }
484 }
485 }
486}
487
488impl Connect<Unresolved> {
489 fn resolve(&self, resolver: &CfgPathResolver) -> Result<Connect<Resolved>, ResolveError> {
491 let socket = self.socket.resolve(resolver)?;
492 let socket_canonical = self
493 .socket_canonical
494 .as_ref()
495 .map(|sc| sc.resolve(resolver))
496 .transpose()?;
497 let auth = self.auth.resolve(resolver)?;
498 let socket_address_file = self
499 .socket_address_file
500 .as_ref()
501 .map(|p| p.path(resolver))
502 .transpose()?;
503 Connect {
504 socket,
505 socket_canonical,
506 auth,
507 socket_address_file,
508 superuser: self.superuser,
509 }
510 .validate()
511 }
512}
513
514impl Connect<Resolved> {
515 fn validate(self) -> Result<Self, ResolveError> {
517 use general::SocketAddr::{Inet, Unix};
518 for bind_addr in self.socket.bind_to_addresses() {
519 match (bind_addr, &self.auth) {
520 (Inet(addr), _) if !addr.ip().is_loopback() => {
521 return Err(ResolveError::AddressNotLoopback);
522 }
523 (Inet(_), Auth::None) => return Err(ResolveError::AuthNotCompatible),
524 (_, Auth::Unrecognized(_)) => return Err(ResolveError::AuthNotRecognized),
525 (Inet(_), Auth::Cookie { .. }) => {}
526 (Unix(_), _) => {}
527 (_, _) => return Err(ResolveError::AddressTypeNotRecognized),
528 };
529 }
530 if self.socket.is_auto() != self.socket_address_file.is_some() {
531 return Err(ResolveError::AutoIncompatibleWithSocketFile);
532 }
533 self.check_absolute_paths()?;
534 Ok(self)
535 }
536
537 fn check_absolute_paths(&self) -> Result<(), ResolveError> {
539 for bind_addr in self.socket.bind_to_addresses() {
540 sockaddr_check_absolute(&bind_addr)?;
541 }
542 if let Some(sa) = &self.socket_canonical {
543 sockaddr_check_absolute(sa.as_ref())?;
544 }
545 self.auth.check_absolute_paths()?;
546 if self
547 .socket_address_file
548 .as_ref()
549 .is_some_and(|p| !p.is_absolute())
550 {
551 return Err(ResolveError::PathNotAbsolute);
552 }
553 Ok(())
554 }
555}
556
557#[derive(Deserialize, Clone, Debug)]
560#[serde(rename_all = "lowercase")]
561pub(crate) enum Auth<R: Addresses> {
562 None,
564 Cookie {
566 path: R::Path,
568 },
569 #[serde(untagged)]
574 Unrecognized(toml::Value),
575}
576
577impl Auth<Unresolved> {
578 fn resolve(&self, resolver: &CfgPathResolver) -> Result<Auth<Resolved>, ResolveError> {
580 match self {
581 Auth::None => Ok(Auth::None),
582 Auth::Cookie { path } => Ok(Auth::Cookie {
583 path: path.path(resolver)?,
584 }),
585 Auth::Unrecognized(x) => Ok(Auth::Unrecognized(x.clone())),
586 }
587 }
588}
589
590impl Auth<Resolved> {
591 fn check_absolute_paths(&self) -> Result<(), ResolveError> {
593 match self {
594 Auth::None => Ok(()),
595 Auth::Cookie { path } => {
596 if path.is_absolute() {
597 Ok(())
598 } else {
599 Err(ResolveError::PathNotAbsolute)
600 }
601 }
602 Auth::Unrecognized(_) => Ok(()),
603 }
604 }
605}
606
607#[derive(Clone, Debug)]
611struct Unresolved;
612impl Addresses for Unresolved {
613 type SocketAddr = String;
614 type Path = CfgPath;
615}
616
617#[derive(Clone, Debug)]
621pub(crate) struct Resolved;
622impl Addresses for Resolved {
623 type SocketAddr = general::SocketAddr;
624 type Path = PathBuf;
625}
626
627#[derive(
632 Clone, Debug, derive_more::AsRef, serde_with::DeserializeFromStr, serde_with::SerializeDisplay,
633)]
634pub(crate) struct AddrWithStr<A>
635where
636 A: Clone + Debug,
637{
638 string: String,
644 #[as_ref]
646 addr: A,
647}
648impl<A> AddrWithStr<A>
649where
650 A: Clone + Debug,
651{
652 pub(crate) fn as_str(&self) -> &str {
655 self.string.as_str()
656 }
657
658 pub(crate) fn set_string_from<B: Clone + Debug>(&mut self, other: &AddrWithStr<B>) {
660 self.string = other.string.clone();
661 }
662}
663impl AddrWithStr<String> {
664 pub(crate) fn resolve(
666 &self,
667 resolver: &CfgPathResolver,
668 ) -> Result<AddrWithStr<general::SocketAddr>, ResolveError> {
669 let AddrWithStr { string, addr } = self;
670 let addr: CfgAddr = addr.parse()?;
671 let substituted = addr.substitutions_will_apply();
672 let addr = addr.address(resolver)?;
673 let string = if substituted {
674 addr.try_to_string().ok_or(ResolveError::PathNotString)?
675 } else {
676 string.clone()
677 };
678 Ok(AddrWithStr { string, addr })
679 }
680}
681impl<A> FromStr for AddrWithStr<A>
682where
683 A: Clone + Debug + FromStr,
684{
685 type Err = <A as FromStr>::Err;
686
687 fn from_str(s: &str) -> Result<Self, Self::Err> {
688 let addr = s.parse()?;
689 let string = s.to_owned();
690 Ok(Self { string, addr })
691 }
692}
693
694impl<A> std::fmt::Display for AddrWithStr<A>
695where
696 A: Clone + Debug + std::fmt::Display,
697{
698 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
699 write!(f, "{}", self.string)
700 }
701}
702
703fn sockaddr_check_absolute(s: &general::SocketAddr) -> Result<(), ResolveError> {
707 match s {
708 general::SocketAddr::Inet(_) => Ok(()),
709 general::SocketAddr::Unix(sa) => match sa.as_pathname() {
710 Some(p) if !p.is_absolute() => Err(ResolveError::PathNotAbsolute),
711 _ => Ok(()),
712 },
713 _ => Err(ResolveError::AddressTypeNotRecognized),
714 }
715}
716
717#[cfg(test)]
718mod test {
719 #![allow(clippy::bool_assert_comparison)]
721 #![allow(clippy::clone_on_copy)]
722 #![allow(clippy::dbg_macro)]
723 #![allow(clippy::mixed_attributes_style)]
724 #![allow(clippy::print_stderr)]
725 #![allow(clippy::print_stdout)]
726 #![allow(clippy::single_char_pattern)]
727 #![allow(clippy::unwrap_used)]
728 #![allow(clippy::unchecked_time_subtraction)]
729 #![allow(clippy::useless_vec)]
730 #![allow(clippy::needless_pass_by_value)]
731 use super::*;
734 use assert_matches::assert_matches;
735
736 fn parse(s: &str) -> ParsedConnectPoint {
737 s.parse().unwrap()
738 }
739
740 #[test]
741 fn examples() {
742 let _e1 = parse(
743 r#"
744[builtin]
745builtin = "abort"
746"#,
747 );
748
749 let _e2 = parse(
750 r#"
751[connect]
752socket = "unix:/var/run/arti/rpc_socket"
753auth = "none"
754"#,
755 );
756
757 let _e3 = parse(
758 r#"
759[connect]
760socket = "inet:[::1]:9191"
761socket_canonical = "inet:[::1]:2020"
762
763auth = { cookie = { path = "/home/user/.arti_rpc/cookie" } }
764"#,
765 );
766
767 let _e4 = parse(
768 r#"
769[connect]
770socket = "inet:[::1]:9191"
771socket_canonical = "inet:[::1]:2020"
772
773[connect.auth.cookie]
774path = "/home/user/.arti_rpc/cookie"
775"#,
776 );
777 }
778
779 #[test]
780 fn parse_errors() {
781 let r: Result<ParsedConnectPoint, _> = "not a toml string".parse();
782 assert_matches!(r, Err(ParseError::InvalidConnectPoint(_)));
783
784 let r: Result<ParsedConnectPoint, _> = "[squidcakes]".parse();
785 assert_matches!(r, Err(ParseError::UnrecognizedFormat));
786
787 let r: Result<ParsedConnectPoint, _> = r#"
788[builtin]
789builtin = "abort"
790
791[connect]
792socket = "inet:[::1]:9191"
793socket_canonical = "inet:[::1]:2020"
794
795auth = { cookie = { path = "/home/user/.arti_rpc/cookie" } }
796"#
797 .parse();
798 assert_matches!(r, Err(ParseError::ConflictingMembers));
799 }
800
801 #[test]
802 fn resolve_errors() {
803 let resolver = CfgPathResolver::default();
804
805 let r: ParsedConnectPoint = r#"
806[connect]
807socket = "inet:[::1]:9191"
808socket_canonical = "inet:[::1]:2020"
809
810[connect.auth.esp]
811telekinetic_handshake = 3
812"#
813 .parse()
814 .unwrap();
815 let err = r.resolve(&resolver).err();
816 assert_matches!(err, Some(ResolveError::AuthNotRecognized));
817
818 let r: ParsedConnectPoint = r#"
819[connect]
820socket = "inet:[::1]:9191"
821socket_canonical = "inet:[::1]:2020"
822
823auth = "foo"
824"#
825 .parse()
826 .unwrap();
827 let err = r.resolve(&resolver).err();
828 assert_matches!(err, Some(ResolveError::AuthNotRecognized));
829 }
830}