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