-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented printing and changing of host name.
- Loading branch information
Showing
16 changed files
with
1,638 additions
and
166 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// This file is part of the uutils hostname package. | ||
// | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
|
||
#[cfg(not(target_family = "windows"))] | ||
pub(crate) mod unix; | ||
#[cfg(target_family = "windows")] | ||
pub(crate) mod windows; | ||
|
||
#[cfg(not(target_family = "windows"))] | ||
pub(crate) use unix::{from_argument, from_file}; | ||
#[cfg(target_family = "windows")] | ||
pub(crate) use windows::{from_argument, from_file}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// This file is part of the uutils hostname package. | ||
// | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
|
||
use std::borrow::Cow; | ||
use std::ffi::{CString, OsStr}; | ||
use std::path::Path; | ||
|
||
use uucore::error::UResult; | ||
|
||
use crate::errors::HostNameError; | ||
use crate::net::set_host_name; | ||
use crate::utils::parse_host_name_file; | ||
|
||
pub(crate) fn from_file(path: &Path) -> UResult<()> { | ||
parse_host_name_file(path).map(Cow::Owned).and_then(run) | ||
} | ||
|
||
pub(crate) fn from_argument(host_name: &OsStr) -> UResult<()> { | ||
#[cfg(target_family = "unix")] | ||
let host_name = { | ||
use std::os::unix::ffi::OsStrExt; | ||
Cow::Borrowed(host_name.as_bytes()) | ||
}; | ||
|
||
#[cfg(target_family = "wasm")] | ||
let host_name = { | ||
use std::os::wasm::ffi::OsStrExt; | ||
Cow::Borrowed(host_name.as_bytes()) | ||
}; | ||
|
||
run(host_name) | ||
} | ||
|
||
fn run(mut host_name: Cow<[u8]>) -> UResult<()> { | ||
// Trim white space. | ||
match &mut host_name { | ||
Cow::Borrowed(name) => *name = name.trim_ascii(), | ||
|
||
Cow::Owned(name) => { | ||
while name.first().is_some_and(u8::is_ascii_whitespace) { | ||
name.remove(0); | ||
} | ||
|
||
while name.last().is_some_and(u8::is_ascii_whitespace) { | ||
name.pop(); | ||
} | ||
} | ||
}; | ||
|
||
let host_name = validate_host_name(host_name)?; | ||
|
||
set_host_name(&host_name) | ||
} | ||
|
||
fn validate_host_name(host_name: Cow<[u8]>) -> Result<CString, HostNameError> { | ||
// Rules: | ||
// - The only allowed prefix and suffix characters are alphanumeric. | ||
// - The only allowed characters inside are alphanumeric, '-' and '.'. | ||
// - The following sequences are disallowed: "..", ".-" and "-.". | ||
// | ||
// Reference: RFC 1035: Domain Names - Implementation And Specification, | ||
// section 2.3.1. Preferred name syntax. | ||
|
||
let (Some(first_byte), Some(last_byte)) = (host_name.first(), host_name.last()) else { | ||
return Err(HostNameError::InvalidHostName); // Empty name. | ||
}; | ||
|
||
let is_disallowed_byte = move |b: &u8| !b.is_ascii_alphanumeric() && *b != b'-' && *b != b'.'; | ||
let is_disallowed_seq = move |seq: &[u8]| seq == b".." || seq == b".-" || seq == b"-."; | ||
|
||
if !first_byte.is_ascii_alphanumeric() | ||
|| !last_byte.is_ascii_alphanumeric() | ||
|| host_name.iter().any(is_disallowed_byte) | ||
|| host_name.windows(2).any(is_disallowed_seq) | ||
{ | ||
return Err(HostNameError::InvalidHostName); | ||
} | ||
|
||
let mut host_name = host_name.into_owned(); | ||
host_name.push(0_u8); | ||
Ok(unsafe { CString::from_vec_with_nul_unchecked(host_name) }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// This file is part of the uutils hostname package. | ||
// | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
|
||
use std::ffi::OsStr; | ||
use std::os::windows::ffi::OsStrExt; | ||
use std::path::Path; | ||
|
||
use uucore::error::UResult; | ||
|
||
use crate::errors::HostNameError; | ||
use crate::net::set_host_name; | ||
use crate::utils::parse_host_name_file; | ||
|
||
pub(crate) fn from_file(path: &Path) -> UResult<()> { | ||
let host_name = parse_host_name_file(path)?; | ||
let host_name = std::str::from_utf8(&host_name).map_err(|_r| HostNameError::InvalidHostName)?; | ||
run(host_name.encode_utf16().collect()) | ||
} | ||
|
||
pub(crate) fn from_argument(host_name: &OsStr) -> UResult<()> { | ||
run(host_name.encode_wide().collect()) | ||
} | ||
|
||
fn run(mut host_name: Vec<u16>) -> UResult<()> { | ||
// Trim white space. | ||
while host_name.first().is_some_and(u16_is_ascii_whitespace) { | ||
host_name.remove(0); | ||
} | ||
|
||
while host_name.last().is_some_and(u16_is_ascii_whitespace) { | ||
host_name.pop(); | ||
} | ||
|
||
validate_host_name(&host_name)?; | ||
|
||
host_name.push(0); // Null-terminate. | ||
set_host_name(&host_name) | ||
} | ||
|
||
fn u16_is_ascii_whitespace(ch: &u16) -> bool { | ||
u8::try_from(*ch).is_ok_and(|b| b.is_ascii_whitespace()) | ||
} | ||
|
||
fn u16_is_ascii_alphanumeric(ch: &u16) -> bool { | ||
u8::try_from(*ch).is_ok_and(|b| b.is_ascii_alphanumeric()) | ||
} | ||
|
||
fn validate_host_name(host_name: &[u16]) -> Result<(), HostNameError> { | ||
// Rules: | ||
// - The only allowed prefix and suffix characters are alphanumeric. | ||
// - The only allowed characters inside are alphanumeric, '-' and '.'. | ||
// - The following sequences are disallowed: "..", ".-" and "-.". | ||
// | ||
// Reference: RFC 1035: Domain Names - Implementation And Specification, | ||
// section 2.3.1. Preferred name syntax. | ||
|
||
const DOT_DOT: [u16; 2] = [b'.' as u16, b'.' as u16]; | ||
const DOT_DASH: [u16; 2] = [b'.' as u16, b'-' as u16]; | ||
const DASH_DOT: [u16; 2] = [b'-' as u16, b'.' as u16]; | ||
|
||
let (Some(first_byte), Some(last_byte)) = (host_name.first(), host_name.last()) else { | ||
return Err(HostNameError::InvalidHostName); // Empty name. | ||
}; | ||
|
||
let is_disallowed_byte = move |ch: &u16| { | ||
!u16_is_ascii_alphanumeric(ch) && *ch != (b'-' as u16) && *ch != (b'.' as u16) | ||
}; | ||
let is_disallowed_seq = move |seq: &[u16]| seq == DOT_DOT || seq == DOT_DASH || seq == DASH_DOT; | ||
|
||
if !u16_is_ascii_alphanumeric(first_byte) | ||
|| !u16_is_ascii_alphanumeric(last_byte) | ||
|| host_name.iter().any(is_disallowed_byte) | ||
|| host_name.windows(2).any(is_disallowed_seq) | ||
{ | ||
Err(HostNameError::InvalidHostName) | ||
} else { | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// This file is part of the uutils hostname package. | ||
// | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
|
||
use std::fmt; | ||
|
||
use uucore::error::UError; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub enum HostNameError { | ||
InvalidHostName, | ||
HostNameTooLong, | ||
NoLocalDomainName, | ||
SetHostNameDenied, | ||
#[cfg(not(target_family = "windows"))] | ||
GetNameOrAddrInfo(GetNameOrAddrInfoError), | ||
} | ||
|
||
impl fmt::Display for HostNameError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Self::InvalidHostName => write!(f, "the specified hostname is invalid"), | ||
Self::HostNameTooLong => write!(f, "name too long"), | ||
Self::NoLocalDomainName => write!(f, "local domain name not set"), | ||
Self::SetHostNameDenied => write!(f, "you must be root to change the host name"), | ||
#[cfg(not(target_family = "windows"))] | ||
Self::GetNameOrAddrInfo(r) => write!(f, "{r}"), | ||
} | ||
} | ||
} | ||
|
||
impl UError for HostNameError { | ||
fn code(&self) -> i32 { | ||
1 | ||
} | ||
|
||
fn usage(&self) -> bool { | ||
false | ||
} | ||
} | ||
|
||
impl std::error::Error for HostNameError {} | ||
|
||
#[cfg(not(target_family = "windows"))] | ||
#[derive(Debug, PartialEq, Eq)] | ||
pub struct GetNameOrAddrInfoError(pub(crate) std::ffi::c_int); | ||
|
||
#[cfg(not(target_family = "windows"))] | ||
impl fmt::Display for GetNameOrAddrInfoError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let message = unsafe { libc::gai_strerror(self.0) }; | ||
if message.is_null() { | ||
write!(f, "domain name resolution failed: error {}", self.0) | ||
} else { | ||
let message = unsafe { std::ffi::CStr::from_ptr(message.cast()) }; | ||
if let Ok(message) = message.to_str() { | ||
write!(f, "{message}") | ||
} else { | ||
write!(f, "{message:?}") | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(not(target_family = "windows"))] | ||
impl std::error::Error for GetNameOrAddrInfoError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.