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