]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-bus: allow numerical uids in -M user@.host 20157/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 7 Jul 2021 16:02:50 +0000 (18:02 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 9 Jul 2021 09:18:21 +0000 (11:18 +0200)
UIDs don't work well over ssh, but locally or with containers they are OK.
In particular, user@.service uses UIDs as identifiers, and it's nice to be
able to copy&paste that UID for interaction with the user's managers.

src/libsystemd/sd-bus/sd-bus.c

index a32e2f5e208524f197c7ed2fd98ec784ed6c888a..6960161c3658e3df85b24e301adbd75287080213 100644 (file)
@@ -39,6 +39,7 @@
 #include "parse-util.h"
 #include "path-util.h"
 #include "process-util.h"
+#include "stdio-util.h"
 #include "string-util.h"
 #include "strv.h"
 #include "user-util.h"
@@ -1617,7 +1618,7 @@ static int user_and_machine_valid(const char *user_and_machine) {
                 if (!user)
                         return -ENOMEM;
 
-                if (!isempty(user) && !valid_user_group_name(user, VALID_USER_RELAX))
+                if (!isempty(user) && !valid_user_group_name(user, VALID_USER_RELAX | VALID_USER_ALLOW_NUMERIC))
                         return false;
 
                 h++;
@@ -1648,17 +1649,25 @@ static int user_and_machine_equivalent(const char *user_and_machine) {
 
         /* Otherwise, if we are root, then we can also allow the ".host" syntax, as that's the user this
          * would connect to. */
-        if (geteuid() == 0 && STR_IN_SET(user_and_machine, ".host", "root@.host"))
+        uid_t uid = geteuid();
+
+        if (uid == 0 && STR_IN_SET(user_and_machine, ".host", "root@.host", "0@.host"))
                 return true;
 
-        /* Otherwise, we have to figure our user name, and compare things with that. */
-        un = getusername_malloc();
-        if (!un)
-                return -ENOMEM;
+        /* Otherwise, we have to figure out our user id and name, and compare things with that. */
+        char buf[DECIMAL_STR_MAX(uid_t)];
+        xsprintf(buf, UID_FMT, uid);
+
+        f = startswith(user_and_machine, buf);
+        if (!f) {
+                un = getusername_malloc();
+                if (!un)
+                        return -ENOMEM;
 
-        f = startswith(user_and_machine, un);
-        if (!f)
-                return false;
+                f = startswith(user_and_machine, un);
+                if (!f)
+                        return false;
+        }
 
         return STR_IN_SET(f, "@", "@.host");
 }