]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hostname-util: introduce machine_spec_valid()
authorMike Yuan <me@yhndnzj.com>
Sun, 15 Jun 2025 17:18:37 +0000 (19:18 +0200)
committerMike Yuan <me@yhndnzj.com>
Mon, 16 Jun 2025 23:39:57 +0000 (01:39 +0200)
src/basic/hostname-util.c
src/basic/hostname-util.h

index 047f0c07355f015d9af65a93cd01c2a6190538da..a3f820e3c9e7e5227fd7f92e9f5c05b1cf42311d 100644 (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;
+}
index 4abdfdd76a1bdb010154344ab96992f3f1433108..5f3930756d047c60e3658a1aa44b314e65f3fd3c 100644 (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);