diff --git a/src/commands/signzone.rs b/src/commands/signzone.rs index c02372e..d2dc18e 100644 --- a/src/commands/signzone.rs +++ b/src/commands/signzone.rs @@ -926,7 +926,8 @@ impl SignZone { let mut writer = if out_file.as_os_str() == "-" { FileOrStdout::Stdout(env.stdout()) } else { - let file = File::create(env.in_cwd(&out_file))?; + let file = File::create(env.in_cwd(&out_file)) + .map_err(|e| format!("unable to create file {}: {e}", out_file.display()))?; let file = BufWriter::new(file); FileOrStdout::File(file) }; @@ -1410,14 +1411,27 @@ impl SignZone { // bytes than are needed. Instead control the allocation size based on // our knowledge of the file size. let mut zone_file = File::open(zonefile_path) - .map_err(Error::from) + .map_err(|e| format!("error opening file: {e}").into()) .context(&format!( "loading zone file from path '{}'", zonefile_path.display(), ))?; - let zone_file_len = zone_file.metadata()?.len(); + let zone_file_len = zone_file + .metadata() + .map_err(|e| { + format!( + "error getting metadata from zonefile {}: {e}", + zonefile_path.display() + ) + })? + .len(); let mut buf = inplace::Zonefile::with_capacity(zone_file_len as usize).writer(); - std::io::copy(&mut zone_file, &mut buf)?; + std::io::copy(&mut zone_file, &mut buf).map_err(|e| { + format!( + "error copying from zonefile {}: {e}", + zonefile_path.display() + ) + })?; let mut reader = buf.into_inner(); if let Some(origin) = &self.origin { @@ -1526,7 +1540,7 @@ impl SignZone { fn load_private_key(key_path: &Path) -> Result { let private_data = std::fs::read_to_string(key_path) - .map_err(Error::from) + .map_err(|e| format!("error reading from file: {e}").into()) .context(&format!( "loading private key from file '{}'", key_path.display(), @@ -1549,7 +1563,7 @@ impl SignZone { fn load_public_key(key_path: &Path) -> Result, Dnskey>, Error> { let public_data = std::fs::read_to_string(key_path) - .map_err(Error::from) + .map_err(|e| format!("error reading from file: {e}").into()) .context(&format!( "loading public key from file '{}'", key_path.display(), diff --git a/src/commands/update.rs b/src/commands/update.rs index a4278b8..6c7cc0b 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -234,16 +234,14 @@ impl Update { let mut records = Vec::new(); for arg in args { if let Some((name, typ)) = arg.split_once(' ') { - let typ = Rtype::from_str(typ).map_err(|e| -> Error { - format!("Invalid resource record type '{typ}': {e}").into() - })?; - let uncertain = UncertainName::>::from_str(name).map_err(|e| -> Error { - format!("Invalid domain name '{name}': {e}").into() - })?; + let typ = Rtype::from_str(typ) + .map_err(|e| format!("Invalid resource record type '{typ}': {e}"))?; + let uncertain = UncertainName::>::from_str(name) + .map_err(|e| format!("Invalid domain name '{name}': {e}"))?; let name = uncertain .chain(origin) - .map_err(|_| -> Error { - format!("Combining {name}.{origin} is too long for a domain name").into() + .map_err(|_| { + format!("Combining {name}.{origin} is too long for a domain name") })? .to_name(); records.push((name, typ)) @@ -264,12 +262,10 @@ impl Update { let mut names = Vec::new(); for name in args { let uncertain = UncertainName::>::from_str(name) - .map_err(|e| -> Error { format!("Invalid domain name '{name}': {e}").into() })?; + .map_err(|e| format!("Invalid domain name '{name}': {e}"))?; let name = uncertain .chain(origin) - .map_err(|_| -> Error { - format!("Combining {name}.{origin} is too long for a domain name").into() - })? + .map_err(|_| format!("Combining {name}.{origin} is too long for a domain name"))? .to_name(); names.push(name) } @@ -482,9 +478,7 @@ impl Update { debug!("Adding prerequisite (RRset exists): RR with name '{domain}' and type '{rtype}'"); prereq_section .push(Self::create_prereq_rrset_exists(domain, rtype)) - .map_err(|e| -> Error { - format!("Failed to add RR to UPDATE message: {e}").into() - })? + .map_err(|e| format!("Failed to add RR to UPDATE message: {e}"))? } } if let Some(rrset_exists_exact) = prerequisites.rrset_exists_exact { @@ -498,9 +492,9 @@ impl Update { // - NAME and TYPE are that of the RRset being denoted. // - CLASS is that of the zone. // - TTL must be specified as zero (0) [...] - prereq_section.push(rr).map_err(|e| -> Error { - format!("Failed to add RR to UPDATE message: {e}").into() - })? + prereq_section + .push(rr) + .map_err(|e| format!("Failed to add RR to UPDATE message: {e}"))? } } if let Some(rrset_non_existent) = prerequisites.rrset_non_existent { @@ -508,9 +502,7 @@ impl Update { debug!("Adding prerequisite (RRset does not exist): RR with name '{domain}' and type '{rtype}'"); prereq_section .push(Self::create_prereq_rrset_non_existent(domain, rtype)) - .map_err(|e| -> Error { - format!("Failed to add RR to UPDATE message: {e}").into() - })? + .map_err(|e| format!("Failed to add RR to UPDATE message: {e}"))? } } if let Some(name_in_use) = prerequisites.name_in_use { @@ -518,9 +510,7 @@ impl Update { debug!("Adding prerequisite (Name in use): with name '{domain}'"); prereq_section .push(Self::create_prereq_name_in_use(domain)) - .map_err(|e| -> Error { - format!("Failed to add RR to UPDATE message: {e}").into() - })? + .map_err(|e| format!("Failed to add RR to UPDATE message: {e}"))? } } if let Some(name_not_in_use) = prerequisites.name_not_in_use { @@ -528,9 +518,7 @@ impl Update { debug!("Adding prerequisite (Name not in use): with name '{domain}'"); prereq_section .push(Self::create_prereq_name_not_in_use(domain)) - .map_err(|e| -> Error { - format!("Failed to add RR to UPDATE message: {e}").into() - })? + .map_err(|e| format!("Failed to add RR to UPDATE message: {e}"))? } } Ok(()) @@ -657,7 +645,10 @@ impl Update { if let Some(nsnames) = nsnames { for name in nsnames { - let found_ips = resolver.lookup_host(&name).await?; + let found_ips = resolver + .lookup_host(&name) + .await + .map_err(|e| format!("unable to lookup addresses for {name}: {e}"))?; for socket in found_ips.port_iter(53) { let resp = match connect_and_send_request(&env, socket, &msg, &tsig_key).await { Ok(resp) => resp, @@ -1406,7 +1397,10 @@ impl LdnsUpdate { .transpose()?; for name in nsnames { - let found_ips = resolver.lookup_host(&name).await?; + let found_ips = resolver + .lookup_host(&name) + .await + .map_err(|e| format!("unable to look up addresses for {name}: {e}"))?; for socket in found_ips.port_iter(53) { let local: SocketAddr = if socket.is_ipv4() { ([0u8; 4], 0).into() @@ -1465,7 +1459,8 @@ mod update_helpers { debug!("Querying resolver for SOA of {zone}"); let response = resolver .query(Question::new(&zone, Rtype::SOA, Class::IN)) - .await?; + .await + .map_err(|e| format!("unable to lookup {zone}/SOA: {e}"))?; debug!("Reading response from resolver"); let mut answer = response.answer()?.limit_to::>(); @@ -1496,7 +1491,8 @@ mod update_helpers { // Step 1 - first find a name server that should know *something* let response = resolver .query(Question::new(&name, Rtype::SOA, Class::IN)) - .await?; + .await + .map_err(|e| format!("unable to lookup {name}/SOA: {e}"))?; debug!("Reading response from resolver"); // We look in both the answer and authority sections. @@ -1515,7 +1511,10 @@ mod update_helpers { debug!("Querying for the IP address of {soa_mname}"); // Step 2 - find SOA MNAME IP address, add to resolver - let response = resolver.lookup_host(&soa_mname).await?; + let response = resolver + .lookup_host(&soa_mname) + .await + .map_err(|e| format!("unable to look up addresses for {soa_mname}: {e}"))?; let Some(ipaddr) = response.iter().next() else { return Err(format!("No A or AAAA record found for {soa_mname}").into()); @@ -1536,7 +1535,8 @@ mod update_helpers { debug!("Querying primary name server directly for SOA of {name}"); let response = resolver .query(Question::new(&name, Rtype::SOA, Class::IN)) - .await?; + .await + .map_err(|e| format!("unable to lookup {name}/SOA: {e}"))?; debug!("Reading response from primary name server"); // We look in both the answer and authority sections. @@ -1572,7 +1572,8 @@ mod update_helpers { .stub_resolver() .await .query(Question::new(&zone, Rtype::NS, Class::IN)) - .await?; + .await + .map_err(|e| format!("unable to lookup {zone}/NS: {e}"))?; debug!("Reading response from {mname}"); let mut nsnames = response diff --git a/src/error.rs b/src/error.rs index 948d6a8..84f7dd9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,4 @@ use std::fmt; -use std::io; use domain::base::wire::ParseError; use tracing::error; @@ -117,12 +116,6 @@ impl From for Error { } } -impl From for Error { - fn from(error: io::Error) -> Self { - Self::new(&error.to_string()) - } -} - impl From for Error { fn from(error: ParseError) -> Self { Self::new(&error.to_string())