hostname-util: introduce machine_spec_valid()

This commit is contained in:
Mike Yuan
2025-06-17 01:39:57 +02:00
parent d2e727bf68
commit 4c6fbe733b
2 changed files with 23 additions and 0 deletions
+22
View File
@@ -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;
}
+1
View File
@@ -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);