Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ jobs:
## flags: IntegrationTests, UnitTests, ${{ steps.vars.outputs.CODECOV_FLAGS }}
flags: ${{ steps.vars.outputs.CODECOV_FLAGS }}
name: codecov-umbrella
fail_ci_if_error: false
fail_ci_if_error: false
109 changes: 109 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ uucore = { workspace = true }

#
hostname = { optional = true, version = "0.0.1", package = "uu_hostname", path = "src/uu/hostname" }
predicates = "3.1.4"
assert_cmd = "2.1.2"

[dev-dependencies]
ctor = { workspace = true }
Expand All @@ -91,6 +93,22 @@ phf_codegen = { workspace = true }
name = "hostname"
path = "src/bin/hostname.rs"

[[bin]]
name = "domainname"
path = "src/uu/domainname/src/main.rs"

[[bin]]
name = "dnsdomainname"
path = "src/uu/dnsdomainname/src/main.rs"

[[bin]]
name = "nisdomainname"
path = "src/uu/nisdomainname/src/main.rs"

[[bin]]
name= "ypdomainname"
path = "src/uu/ypdomainname/src/main.rs"

[[bin]]
name = "uudoc"
path = "src/bin/uudoc.rs"
Expand Down
12 changes: 12 additions & 0 deletions src/uu/dnsdomainname/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Wrapper binary: dnsdomainname -> hostname -d
fn main() {
uucore::panic::mute_sigpipe_panic();

use std::ffi::OsString;
use std::process;

let args = std::env::args_os().skip(1);
let iter = (vec![OsString::from("hostname"), OsString::from("-d")].into_iter()).chain(args);

process::exit(hostname::uumain(iter));
}
12 changes: 12 additions & 0 deletions src/uu/domainname/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Wrapper binary: domainname -> hostname -y
fn main() {
uucore::panic::mute_sigpipe_panic();

use std::ffi::OsString;
use std::process;

let args = std::env::args_os().skip(1);
let iter = (vec![OsString::from("hostname"), OsString::from("-y")].into_iter()).chain(args);

process::exit(hostname::uumain(iter));
}
2 changes: 1 addition & 1 deletion src/uu/hostname/src/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ pub(crate) mod unix;
pub(crate) mod windows;

#[cfg(not(target_family = "windows"))]
pub(crate) use unix::{from_argument, from_file};
pub(crate) use unix::{from_argument, from_argument_nis, from_file, from_file_nis};
#[cfg(target_family = "windows")]
pub(crate) use windows::{from_argument, from_file};
48 changes: 47 additions & 1 deletion src/uu/hostname/src/change/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ use std::path::Path;
use uucore::error::UResult;

use crate::errors::HostNameError;
use crate::net::set_host_name;
use crate::net::{set_domain_name, 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_file_nis(path: &Path) -> UResult<()> {
parse_host_name_file(path).map(Cow::Owned).and_then(run_nis)
}

pub(crate) fn from_argument(host_name: &OsStr) -> UResult<()> {
#[cfg(target_family = "unix")]
let host_name = {
Expand All @@ -33,6 +37,22 @@ pub(crate) fn from_argument(host_name: &OsStr) -> UResult<()> {
run(host_name)
}

pub(crate) fn from_argument_nis(domain_name: &OsStr) -> UResult<()> {
#[cfg(target_family = "unix")]
let domain_name = {
use std::os::unix::ffi::OsStrExt;
Cow::Borrowed(domain_name.as_bytes())
};

#[cfg(target_family = "wasm")]
let domain_name = {
use std::os::wasm::ffi::OsStrExt;
Cow::Borrowed(domain_name.as_bytes())
};

run_nis(domain_name)
}

fn run(mut host_name: Cow<[u8]>) -> UResult<()> {
// Trim white space.
match &mut host_name {
Expand All @@ -54,6 +74,32 @@ fn run(mut host_name: Cow<[u8]>) -> UResult<()> {
set_host_name(&host_name)
}

fn run_nis(mut domain_name: Cow<[u8]>) -> UResult<()> {
match &mut domain_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 domain_name = validate_domain_name(domain_name)?;
set_domain_name(&domain_name)
}

fn validate_domain_name(domain_name: Cow<[u8]>) -> Result<CString, HostNameError> {
if domain_name.is_empty() {
return Err(HostNameError::InvalidHostName);
}
CString::new(domain_name.into_owned()).map_err(|_| HostNameError::InvalidHostName)
}

fn validate_host_name(host_name: Cow<[u8]>) -> Result<CString, HostNameError> {
// Rules:
// - The only allowed prefix and suffix characters are alphanumeric.
Expand Down
4 changes: 4 additions & 0 deletions src/uu/hostname/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub enum HostNameError {
NoLocalDomainName,
SetHostNameDenied,
#[cfg(not(target_family = "windows"))]
SetDomainNameDenied,
#[cfg(not(target_family = "windows"))]
GetNameOrAddrInfo(GetNameOrAddrInfoError),
}

Expand All @@ -25,6 +27,8 @@ impl fmt::Display for HostNameError {
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::SetDomainNameDenied => write!(f, "you must be root to change the domain name"),
#[cfg(not(target_family = "windows"))]
Self::GetNameOrAddrInfo(r) => write!(f, "{r}"),
}
}
Expand Down
18 changes: 16 additions & 2 deletions src/uu/hostname/src/hostname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,24 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

let _net_lib_guard = net::LibraryGuard::load()?;

if args.contains_id("set-group") {
if args.contains_id(options::FILE) || args.contains_id(options::HOSTNAME) {
#[cfg(not(target_family = "windows"))]
let nis_mode = args.get_flag(options::NIS);
if let Some(path) = args.get_one::<PathBuf>(options::FILE) {
#[cfg(not(target_family = "windows"))]
if nis_mode {
return change::from_file_nis(path);
}
change::from_file(path)
} else {
let host_name = args
.get_one::<OsString>(options::HOSTNAME)
.expect("hostname must be specified");

#[cfg(not(target_family = "windows"))]
if nis_mode {
return change::from_argument_nis(host_name);
}
change::from_argument(host_name)
}
} else {
Expand Down Expand Up @@ -176,11 +186,15 @@ hostname {-V|--version}";
options::IP_ADDRESS,
options::ALL_IP_ADDRESSES,
options::SHORT,
options::NIS,
])
.multiple(false)
.conflicts_with("set-group"),
)
.group(
ArgGroup::new("nis-group")
.args([options::NIS])
.conflicts_with("get-group"),
)
.group(
ArgGroup::new("set-group")
.args([options::BOOT, options::FILE, options::HOSTNAME])
Expand Down
Loading
Loading