From: Mike Yuan Date: Sun, 15 Jun 2025 17:18:37 +0000 (+0200) Subject: hostname-util: introduce machine_spec_valid() X-Git-Tag: v258-rc1~307^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4c6fbe733bc5222f1b898710cdf660b6a0d5ca9d;p=thirdparty%2Fsystemd.git hostname-util: introduce machine_spec_valid() --- diff --git a/src/basic/hostname-util.c b/src/basic/hostname-util.c index 047f0c07355..a3f820e3c9e 100644 --- a/src/basic/hostname-util.c +++ b/src/basic/hostname-util.c @@ -9,6 +9,7 @@ #include "os-util.h" #include "string-util.h" #include "strv.h" +#include "user-util.h" char* get_default_hostname_raw(void) { int r; @@ -213,3 +214,24 @@ int split_user_at_host(const char *s, char **ret_user, char **ret_host) { return !!rhs; /* return > 0 if '@' was specified, 0 otherwise */ } + +int machine_spec_valid(const char *s) { + _cleanup_free_ char *u = NULL, *h = NULL; + int r; + + assert(s); + + r = split_user_at_host(s, &u, &h); + if (r == -EINVAL) + return false; + if (r < 0) + return r; + + if (u && !valid_user_group_name(u, VALID_USER_RELAX | VALID_USER_ALLOW_NUMERIC)) + return false; + + if (h && !hostname_is_valid(h, VALID_HOSTNAME_DOT_HOST)) + return false; + + return true; +} diff --git a/src/basic/hostname-util.h b/src/basic/hostname-util.h index 4abdfdd76a1..5f3930756d0 100644 --- a/src/basic/hostname-util.h +++ b/src/basic/hostname-util.h @@ -39,4 +39,5 @@ static inline bool is_dns_proxy_stub_hostname(const char *hostname) { int get_pretty_hostname(char **ret); +int machine_spec_valid(const char *s); int split_user_at_host(const char *s, char **ret_user, char **ret_host);