ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
8be66a
From 1e4ec1b29d15684a305bbc9ab54c6c8321504e7b Mon Sep 17 00:00:00 2001
8be66a
From: David Tardon <dtardon@redhat.com>
8be66a
Date: Tue, 27 Oct 2020 10:31:05 +0100
8be66a
Subject: [PATCH] shared/user-util: add compat forms of user name checking
8be66a
 functions
8be66a
8be66a
New functions are called valid_user_group_name_compat() and
8be66a
valid_user_group_name_or_id_compat() and accept dots in the user
8be66a
or group name. No functional change except the tests.
8be66a
8be66a
(cherry picked from commit 1a29610f5fa1bcb2eeb37d2c6b79d8d1a6dbb865)
8be66a
8be66a
This completes previous partial cherry-pick of the same commit (commit
8be66a
76176de0889c3e8b9b3a176da24e4f8dbbd380a3).
8be66a
8be66a
Related: #1848373
8be66a
---
8be66a
 src/basic/user-util.c     | 32 +++++++-------
8be66a
 src/basic/user-util.h     | 16 ++++++-
8be66a
 src/test/test-user-util.c | 91 +++++++++++++++++++++++++++++++++++++--
8be66a
 3 files changed, 117 insertions(+), 22 deletions(-)
8be66a
8be66a
diff --git a/src/basic/user-util.c b/src/basic/user-util.c
8be66a
index 40f4e45db6..03cbbc2503 100644
8be66a
--- a/src/basic/user-util.c
8be66a
+++ b/src/basic/user-util.c
8be66a
@@ -576,7 +576,7 @@ int take_etc_passwd_lock(const char *root) {
8be66a
         return fd;
8be66a
 }
8be66a
 
8be66a
-bool valid_user_group_name(const char *u) {
8be66a
+bool valid_user_group_name_full(const char *u, bool strict) {
8be66a
         const char *i;
8be66a
         long sz;
8be66a
 
8be66a
@@ -585,12 +585,12 @@ bool valid_user_group_name(const char *u) {
8be66a
          *
8be66a
          * - We require that names fit into the appropriate utmp field
8be66a
          * - We don't allow empty user names
8be66a
+         * - No dots or digits in the first character
8be66a
          *
8be66a
-         * Note that other systems are even more restrictive, and don't permit underscores or uppercase characters.
8be66a
+         * If strict==true, additionally:
8be66a
+         * - We don't allow any dots (this conflicts with chown syntax which permits dots as user/group name separator)
8be66a
          *
8be66a
-         * jsynacek: We now allow dots in user names. The checks are not exhaustive as user names like "..." are allowed
8be66a
-         * and valid according to POSIX, but can't be created using useradd. However, ".user" can be created. Let's not
8be66a
-         * complicate the code by adding additional checks for weird corner cases like these,  as they don't really matter here.
8be66a
+         * Note that other systems are even more restrictive, and don't permit underscores or uppercase characters.
8be66a
          */
8be66a
 
8be66a
         if (isempty(u))
8be66a
@@ -598,16 +598,16 @@ bool valid_user_group_name(const char *u) {
8be66a
 
8be66a
         if (!(u[0] >= 'a' && u[0] <= 'z') &&
8be66a
             !(u[0] >= 'A' && u[0] <= 'Z') &&
8be66a
-            u[0] != '_' && u[0] != '.')
8be66a
+            u[0] != '_')
8be66a
                 return false;
8be66a
 
8be66a
-        for (i = u+1; *i; i++) {
8be66a
-                if (!(*i >= 'a' && *i <= 'z') &&
8be66a
-                    !(*i >= 'A' && *i <= 'Z') &&
8be66a
-                    !(*i >= '0' && *i <= '9') &&
8be66a
-                    !IN_SET(*i, '_', '-', '.'))
8be66a
+        for (i = u+1; *i; i++)
8be66a
+                if (!((*i >= 'a' && *i <= 'z') ||
8be66a
+                      (*i >= 'A' && *i <= 'Z') ||
8be66a
+                      (*i >= '0' && *i <= '9') ||
8be66a
+                      IN_SET(*i, '_', '-') ||
8be66a
+                      (!strict && *i == '.')))
8be66a
                         return false;
8be66a
-        }
8be66a
 
8be66a
         sz = sysconf(_SC_LOGIN_NAME_MAX);
8be66a
         assert_se(sz > 0);
8be66a
@@ -621,15 +621,15 @@ bool valid_user_group_name(const char *u) {
8be66a
         return true;
8be66a
 }
8be66a
 
8be66a
-bool valid_user_group_name_or_id(const char *u) {
8be66a
+bool valid_user_group_name_or_id_full(const char *u, bool strict) {
8be66a
 
8be66a
-        /* Similar as above, but is also fine with numeric UID/GID specifications, as long as they are in the right
8be66a
-         * range, and not the invalid user ids. */
8be66a
+        /* Similar as above, but is also fine with numeric UID/GID specifications, as long as they are in the
8be66a
+         * right range, and not the invalid user ids. */
8be66a
 
8be66a
         if (isempty(u))
8be66a
                 return false;
8be66a
 
8be66a
-        if (valid_user_group_name(u))
8be66a
+        if (valid_user_group_name_full(u, strict))
8be66a
                 return true;
8be66a
 
8be66a
         return parse_uid(u, NULL) >= 0;
8be66a
diff --git a/src/basic/user-util.h b/src/basic/user-util.h
8be66a
index b74f168859..5ad0b2a2f9 100644
8be66a
--- a/src/basic/user-util.h
8be66a
+++ b/src/basic/user-util.h
8be66a
@@ -78,8 +78,20 @@ static inline bool userns_supported(void) {
8be66a
         return access("/proc/self/uid_map", F_OK) >= 0;
8be66a
 }
8be66a
 
8be66a
-bool valid_user_group_name(const char *u);
8be66a
-bool valid_user_group_name_or_id(const char *u);
8be66a
+bool valid_user_group_name_full(const char *u, bool strict);
8be66a
+bool valid_user_group_name_or_id_full(const char *u, bool strict);
8be66a
+static inline bool valid_user_group_name(const char *u) {
8be66a
+        return valid_user_group_name_full(u, true);
8be66a
+}
8be66a
+static inline bool valid_user_group_name_or_id(const char *u) {
8be66a
+        return valid_user_group_name_or_id_full(u, true);
8be66a
+}
8be66a
+static inline bool valid_user_group_name_compat(const char *u) {
8be66a
+        return valid_user_group_name_full(u, false);
8be66a
+}
8be66a
+static inline bool valid_user_group_name_or_id_compat(const char *u) {
8be66a
+        return valid_user_group_name_or_id_full(u, false);
8be66a
+}
8be66a
 bool valid_gecos(const char *d);
8be66a
 bool valid_home(const char *p);
8be66a
 
8be66a
diff --git a/src/test/test-user-util.c b/src/test/test-user-util.c
8be66a
index 04e86f5ac3..3a4211655d 100644
8be66a
--- a/src/test/test-user-util.c
8be66a
+++ b/src/test/test-user-util.c
8be66a
@@ -131,6 +131,43 @@ static void test_uid_ptr(void) {
8be66a
         assert_se(PTR_TO_UID(UID_TO_PTR(1000)) == 1000);
8be66a
 }
8be66a
 
8be66a
+static void test_valid_user_group_name_compat(void) {
8be66a
+        log_info("/* %s */", __func__);
8be66a
+
8be66a
+        assert_se(!valid_user_group_name_compat(NULL));
8be66a
+        assert_se(!valid_user_group_name_compat(""));
8be66a
+        assert_se(!valid_user_group_name_compat("1"));
8be66a
+        assert_se(!valid_user_group_name_compat("65535"));
8be66a
+        assert_se(!valid_user_group_name_compat("-1"));
8be66a
+        assert_se(!valid_user_group_name_compat("-kkk"));
8be66a
+        assert_se(!valid_user_group_name_compat("rööt"));
8be66a
+        assert_se(!valid_user_group_name_compat("."));
8be66a
+        assert_se(!valid_user_group_name_compat(".eff"));
8be66a
+        assert_se(!valid_user_group_name_compat("foo\nbar"));
8be66a
+        assert_se(!valid_user_group_name_compat("0123456789012345678901234567890123456789"));
8be66a
+        assert_se(!valid_user_group_name_or_id_compat("aaa:bbb"));
8be66a
+        assert_se(!valid_user_group_name_compat("."));
8be66a
+        assert_se(!valid_user_group_name_compat(".1"));
8be66a
+        assert_se(!valid_user_group_name_compat(".65535"));
8be66a
+        assert_se(!valid_user_group_name_compat(".-1"));
8be66a
+        assert_se(!valid_user_group_name_compat(".-kkk"));
8be66a
+        assert_se(!valid_user_group_name_compat(".rööt"));
8be66a
+        assert_se(!valid_user_group_name_or_id_compat(".aaa:bbb"));
8be66a
+
8be66a
+        assert_se(valid_user_group_name_compat("root"));
8be66a
+        assert_se(valid_user_group_name_compat("lennart"));
8be66a
+        assert_se(valid_user_group_name_compat("LENNART"));
8be66a
+        assert_se(valid_user_group_name_compat("_kkk"));
8be66a
+        assert_se(valid_user_group_name_compat("kkk-"));
8be66a
+        assert_se(valid_user_group_name_compat("kk-k"));
8be66a
+        assert_se(valid_user_group_name_compat("eff.eff"));
8be66a
+        assert_se(valid_user_group_name_compat("eff."));
8be66a
+
8be66a
+        assert_se(valid_user_group_name_compat("some5"));
8be66a
+        assert_se(!valid_user_group_name_compat("5some"));
8be66a
+        assert_se(valid_user_group_name_compat("INNER5NUMBER"));
8be66a
+}
8be66a
+
8be66a
 static void test_valid_user_group_name(void) {
8be66a
         log_info("/* %s */", __func__);
8be66a
 
8be66a
@@ -141,9 +178,18 @@ static void test_valid_user_group_name(void) {
8be66a
         assert_se(!valid_user_group_name("-1"));
8be66a
         assert_se(!valid_user_group_name("-kkk"));
8be66a
         assert_se(!valid_user_group_name("rööt"));
8be66a
+        assert_se(!valid_user_group_name("."));
8be66a
+        assert_se(!valid_user_group_name(".eff"));
8be66a
         assert_se(!valid_user_group_name("foo\nbar"));
8be66a
         assert_se(!valid_user_group_name("0123456789012345678901234567890123456789"));
8be66a
         assert_se(!valid_user_group_name_or_id("aaa:bbb"));
8be66a
+        assert_se(!valid_user_group_name("."));
8be66a
+        assert_se(!valid_user_group_name(".1"));
8be66a
+        assert_se(!valid_user_group_name(".65535"));
8be66a
+        assert_se(!valid_user_group_name(".-1"));
8be66a
+        assert_se(!valid_user_group_name(".-kkk"));
8be66a
+        assert_se(!valid_user_group_name(".rööt"));
8be66a
+        assert_se(!valid_user_group_name_or_id(".aaa:bbb"));
8be66a
 
8be66a
         assert_se(valid_user_group_name("root"));
8be66a
         assert_se(valid_user_group_name("lennart"));
8be66a
@@ -151,14 +197,47 @@ static void test_valid_user_group_name(void) {
8be66a
         assert_se(valid_user_group_name("_kkk"));
8be66a
         assert_se(valid_user_group_name("kkk-"));
8be66a
         assert_se(valid_user_group_name("kk-k"));
8be66a
-        assert_se(valid_user_group_name(".moo"));
8be66a
-        assert_se(valid_user_group_name("eff.eff"));
8be66a
+        assert_se(!valid_user_group_name("eff.eff"));
8be66a
+        assert_se(!valid_user_group_name("eff."));
8be66a
 
8be66a
         assert_se(valid_user_group_name("some5"));
8be66a
         assert_se(!valid_user_group_name("5some"));
8be66a
         assert_se(valid_user_group_name("INNER5NUMBER"));
8be66a
 }
8be66a
 
8be66a
+static void test_valid_user_group_name_or_id_compat(void) {
8be66a
+        log_info("/* %s */", __func__);
8be66a
+
8be66a
+        assert_se(!valid_user_group_name_or_id_compat(NULL));
8be66a
+        assert_se(!valid_user_group_name_or_id_compat(""));
8be66a
+        assert_se(valid_user_group_name_or_id_compat("0"));
8be66a
+        assert_se(valid_user_group_name_or_id_compat("1"));
8be66a
+        assert_se(valid_user_group_name_or_id_compat("65534"));
8be66a
+        assert_se(!valid_user_group_name_or_id_compat("65535"));
8be66a
+        assert_se(valid_user_group_name_or_id_compat("65536"));
8be66a
+        assert_se(!valid_user_group_name_or_id_compat("-1"));
8be66a
+        assert_se(!valid_user_group_name_or_id_compat("-kkk"));
8be66a
+        assert_se(!valid_user_group_name_or_id_compat("rööt"));
8be66a
+        assert_se(!valid_user_group_name_or_id_compat("."));
8be66a
+        assert_se(!valid_user_group_name_or_id_compat(".eff"));
8be66a
+        assert_se(valid_user_group_name_or_id_compat("eff.eff"));
8be66a
+        assert_se(valid_user_group_name_or_id_compat("eff."));
8be66a
+        assert_se(!valid_user_group_name_or_id_compat("foo\nbar"));
8be66a
+        assert_se(!valid_user_group_name_or_id_compat("0123456789012345678901234567890123456789"));
8be66a
+        assert_se(!valid_user_group_name_or_id_compat("aaa:bbb"));
8be66a
+
8be66a
+        assert_se(valid_user_group_name_or_id_compat("root"));
8be66a
+        assert_se(valid_user_group_name_or_id_compat("lennart"));
8be66a
+        assert_se(valid_user_group_name_or_id_compat("LENNART"));
8be66a
+        assert_se(valid_user_group_name_or_id_compat("_kkk"));
8be66a
+        assert_se(valid_user_group_name_or_id_compat("kkk-"));
8be66a
+        assert_se(valid_user_group_name_or_id_compat("kk-k"));
8be66a
+
8be66a
+        assert_se(valid_user_group_name_or_id_compat("some5"));
8be66a
+        assert_se(!valid_user_group_name_or_id_compat("5some"));
8be66a
+        assert_se(valid_user_group_name_or_id_compat("INNER5NUMBER"));
8be66a
+}
8be66a
+
8be66a
 static void test_valid_user_group_name_or_id(void) {
8be66a
         log_info("/* %s */", __func__);
8be66a
 
8be66a
@@ -172,6 +251,10 @@ static void test_valid_user_group_name_or_id(void) {
8be66a
         assert_se(!valid_user_group_name_or_id("-1"));
8be66a
         assert_se(!valid_user_group_name_or_id("-kkk"));
8be66a
         assert_se(!valid_user_group_name_or_id("rööt"));
8be66a
+        assert_se(!valid_user_group_name_or_id("."));
8be66a
+        assert_se(!valid_user_group_name_or_id(".eff"));
8be66a
+        assert_se(!valid_user_group_name_or_id("eff.eff"));
8be66a
+        assert_se(!valid_user_group_name_or_id("eff."));
8be66a
         assert_se(!valid_user_group_name_or_id("foo\nbar"));
8be66a
         assert_se(!valid_user_group_name_or_id("0123456789012345678901234567890123456789"));
8be66a
         assert_se(!valid_user_group_name_or_id("aaa:bbb"));
8be66a
@@ -182,8 +265,6 @@ static void test_valid_user_group_name_or_id(void) {
8be66a
         assert_se(valid_user_group_name_or_id("_kkk"));
8be66a
         assert_se(valid_user_group_name_or_id("kkk-"));
8be66a
         assert_se(valid_user_group_name_or_id("kk-k"));
8be66a
-        assert_se(valid_user_group_name_or_id(".moo"));
8be66a
-        assert_se(valid_user_group_name_or_id("eff.eff"));
8be66a
 
8be66a
         assert_se(valid_user_group_name_or_id("some5"));
8be66a
         assert_se(!valid_user_group_name_or_id("5some"));
8be66a
@@ -286,7 +367,9 @@ int main(int argc, char*argv[]) {
8be66a
         test_parse_uid();
8be66a
         test_uid_ptr();
8be66a
 
8be66a
+        test_valid_user_group_name_compat();
8be66a
         test_valid_user_group_name();
8be66a
+        test_valid_user_group_name_or_id_compat();
8be66a
         test_valid_user_group_name_or_id();
8be66a
         test_valid_gecos();
8be66a
         test_valid_home();