ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
21255d
From f06434cc51eedd72f7d4a640a1fa118f57a5e68e Mon Sep 17 00:00:00 2001
21255d
From: Balint Reczey <balint.reczey@canonical.com>
21255d
Date: Wed, 18 Mar 2020 18:29:02 +0100
21255d
Subject: [PATCH] user-util: Allow names starting with a digit
21255d
21255d
In 1a29610f5fa1bcb2eeb37d2c6b79d8d1a6dbb865 the change inadvertedly
21255d
disabled names with digit as the first character. This follow-up change
21255d
allows a digit as the first character in compat mode.
21255d
21255d
Fixes: #15141
21255d
(cherry picked from commit 93c23c9297e48e594785e0bb9c51504aae5fbe3e)
21255d
21255d
Related: #1848373
21255d
---
21255d
 src/basic/user-util.c     | 20 +++++++++++++++++---
21255d
 src/test/test-user-util.c |  4 ++--
21255d
 2 files changed, 19 insertions(+), 5 deletions(-)
21255d
21255d
diff --git a/src/basic/user-util.c b/src/basic/user-util.c
21255d
index 359da08a83..7dd2bb2c84 100644
21255d
--- a/src/basic/user-util.c
21255d
+++ b/src/basic/user-util.c
21255d
@@ -579,16 +579,18 @@ int take_etc_passwd_lock(const char *root) {
21255d
 bool valid_user_group_name_full(const char *u, bool strict) {
21255d
         const char *i;
21255d
         long sz;
21255d
+        bool warned = false;
21255d
 
21255d
         /* Checks if the specified name is a valid user/group name. Also see POSIX IEEE Std 1003.1-2008, 2016 Edition,
21255d
          * 3.437. We are a bit stricter here however. Specifically we deviate from POSIX rules:
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
+         * - No dots in the first character
21255d
          *
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
+         * - We don't allow a digit as the first character
21255d
          *
21255d
          * Note that other systems are even more restrictive, and don't permit underscores or uppercase characters.
21255d
          */
21255d
@@ -598,17 +600,26 @@ bool valid_user_group_name_full(const char *u, bool strict) {
21255d
 
21255d
         if (!(u[0] >= 'a' && u[0] <= 'z') &&
21255d
             !(u[0] >= 'A' && u[0] <= 'Z') &&
21255d
+            !(u[0] >= '0' && u[0] <= '9' && !strict) &&
21255d
             u[0] != '_')
21255d
                 return false;
21255d
 
21255d
-        bool warned = false;
21255d
+        bool only_digits_seen = u[0] >= '0' && u[0] <= '9';
21255d
+
21255d
+        if (only_digits_seen) {
21255d
+                log_warning("User or group name \"%s\" starts with a digit, accepting for compatibility.", u);
21255d
+                warned = true;
21255d
+        }
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
+                     IN_SET(*i, '_', '-'))) {
21255d
+                        if (!(*i >= '0' && *i <= '9'))
21255d
+                                only_digits_seen = false;
21255d
                         continue;
21255d
+                        }
21255d
 
21255d
                 if (*i == '.' && !strict) {
21255d
                         if (!warned) {
21255d
@@ -622,6 +633,9 @@ bool valid_user_group_name_full(const char *u, bool strict) {
21255d
                 return false;
21255d
         }
21255d
 
21255d
+        if (only_digits_seen)
21255d
+                return false;
21255d
+
21255d
         sz = sysconf(_SC_LOGIN_NAME_MAX);
21255d
         assert_se(sz > 0);
21255d
 
21255d
diff --git a/src/test/test-user-util.c b/src/test/test-user-util.c
21255d
index 3a4211655d..56079f1486 100644
21255d
--- a/src/test/test-user-util.c
21255d
+++ b/src/test/test-user-util.c
21255d
@@ -164,7 +164,7 @@ static void test_valid_user_group_name_compat(void) {
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("5some"));
21255d
         assert_se(valid_user_group_name_compat("INNER5NUMBER"));
21255d
 }
21255d
 
21255d
@@ -234,7 +234,7 @@ static void test_valid_user_group_name_or_id_compat(void) {
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("5some"));
21255d
         assert_se(valid_user_group_name_or_id_compat("INNER5NUMBER"));
21255d
 }
21255d