Blame SOURCES/0001-Use-password-file-when-creating-admin-user.patch

34ae41
From 5764a80e5edd7fa38323146261c6b4e498d282dd Mon Sep 17 00:00:00 2001
34ae41
From: "Endi S. Dewata" <edewata@redhat.com>
34ae41
Date: Mon, 17 May 2021 18:17:26 -0500
34ae41
Subject: [PATCH] Use password file when creating admin user
34ae41
34ae41
The pki-server <subsystem>-user-add has been updated to
34ae41
provide a --password-file option. The deployment tool
34ae41
has been modified to use this option when creating the
34ae41
admin user to avoid the password from getting logged in
34ae41
the debug mode.
34ae41
34ae41
Resolves: CVE-2021-3551
34ae41
---
34ae41
 base/server/python/pki/server/cli/user.py     |  9 ++-
34ae41
 .../python/pki/server/deployment/__init__.py  |  5 +-
34ae41
 base/server/python/pki/server/subsystem.py    | 74 +++++++++++--------
34ae41
 .../server/cli/SubsystemUserAddCLI.java       | 11 +++
34ae41
 4 files changed, 66 insertions(+), 33 deletions(-)
34ae41
34ae41
diff --git a/base/server/python/pki/server/cli/user.py b/base/server/python/pki/server/cli/user.py
34ae41
index c00a1acb50..c5c8d52956 100644
34ae41
--- a/base/server/python/pki/server/cli/user.py
34ae41
+++ b/base/server/python/pki/server/cli/user.py
34ae41
@@ -47,6 +47,7 @@ class UserAddCLI(pki.cli.CLI):
34ae41
         print('      --full-name <full name>        Full name')
34ae41
         print('      --email <email>                Email')
34ae41
         print('      --password <password>          Password')
34ae41
+        print('      --password-file <path>         Password file')
34ae41
         print('      --phone <phone>                Phone')
34ae41
         print('      --type <type>                  Type')
34ae41
         print('      --state <state>                State')
34ae41
@@ -59,7 +60,8 @@ class UserAddCLI(pki.cli.CLI):
34ae41
     def execute(self, argv):
34ae41
         try:
34ae41
             opts, args = getopt.gnu_getopt(argv, 'i:v', [
34ae41
-                'instance=', 'full-name=', 'email=', 'password=',
34ae41
+                'instance=', 'full-name=', 'email=',
34ae41
+                'password=', 'password-file=',
34ae41
                 'phone=', 'type=', 'state=', 'tps-profiles=',
34ae41
                 'verbose', 'debug', 'help'])
34ae41
 
34ae41
@@ -73,6 +75,7 @@ class UserAddCLI(pki.cli.CLI):
34ae41
         full_name = None
34ae41
         email = None
34ae41
         password = None
34ae41
+        password_file = None
34ae41
         phone = None
34ae41
         user_type = None
34ae41
         state = None
34ae41
@@ -91,6 +94,9 @@ class UserAddCLI(pki.cli.CLI):
34ae41
             elif o == '--password':
34ae41
                 password = a
34ae41
 
34ae41
+            elif o == '--password-file':
34ae41
+                password_file = a
34ae41
+
34ae41
             elif o == '--phone':
34ae41
                 phone = a
34ae41
 
34ae41
@@ -149,6 +155,7 @@ class UserAddCLI(pki.cli.CLI):
34ae41
             full_name=full_name,
34ae41
             email=email,
34ae41
             password=password,
34ae41
+            password_file=password_file,
34ae41
             phone=phone,
34ae41
             user_type=user_type,
34ae41
             tps_profiles=tps_profiles,
34ae41
diff --git a/base/server/python/pki/server/deployment/__init__.py b/base/server/python/pki/server/deployment/__init__.py
34ae41
index 347ab1acdd..6d5f083b47 100644
34ae41
--- a/base/server/python/pki/server/deployment/__init__.py
34ae41
+++ b/base/server/python/pki/server/deployment/__init__.py
34ae41
@@ -373,6 +373,8 @@ class PKIDeployer:
34ae41
 
34ae41
         response = client.setupAdmin(request)
34ae41
 
34ae41
+        # Run the command as current user such that
34ae41
+        # it can read the temporary password file.
34ae41
         subsystem.add_user(
34ae41
             uid,
34ae41
             full_name=full_name,
34ae41
@@ -380,7 +382,8 @@ class PKIDeployer:
34ae41
             password=password,
34ae41
             user_type='adminType',
34ae41
             state='1',
34ae41
-            tps_profiles=tps_profiles)
34ae41
+            tps_profiles=tps_profiles,
34ae41
+            as_current_user=True)
34ae41
 
34ae41
         admin_groups = subsystem.config['preop.admin.group']
34ae41
         groups = [x.strip() for x in admin_groups.split(',')]
34ae41
diff --git a/base/server/python/pki/server/subsystem.py b/base/server/python/pki/server/subsystem.py
34ae41
index a3ed0c7f3a..41d8d67c2e 100644
34ae41
--- a/base/server/python/pki/server/subsystem.py
34ae41
+++ b/base/server/python/pki/server/subsystem.py
34ae41
@@ -1335,54 +1335,66 @@ class PKISubsystem(object):
34ae41
                  full_name=None,
34ae41
                  email=None,
34ae41
                  password=None,
34ae41
+                 password_file=None,
34ae41
                  phone=None,
34ae41
                  user_type=None,
34ae41
                  state=None,
34ae41
                  tps_profiles=None,
34ae41
                  as_current_user=False):
34ae41
 
34ae41
-        cmd = [self.name + '-user-add']
34ae41
+        tmpdir = tempfile.mkdtemp()
34ae41
 
34ae41
-        if full_name:
34ae41
-            cmd.append('--full-name')
34ae41
-            cmd.append(full_name)
34ae41
+        try:
34ae41
+            if password and not password_file:
34ae41
+                password_file = os.path.join(tmpdir, 'password.txt')
34ae41
+                with open(password_file, 'w') as f:
34ae41
+                    f.write(password)
34ae41
 
34ae41
-        if email:
34ae41
-            cmd.append('--email')
34ae41
-            cmd.append(email)
34ae41
+            cmd = [self.name + '-user-add']
34ae41
 
34ae41
-        if password:
34ae41
-            cmd.append('--password')
34ae41
-            cmd.append(password)
34ae41
+            if full_name:
34ae41
+                cmd.append('--full-name')
34ae41
+                cmd.append(full_name)
34ae41
 
34ae41
-        if phone:
34ae41
-            cmd.append('--phone')
34ae41
-            cmd.append(phone)
34ae41
+            if email:
34ae41
+                cmd.append('--email')
34ae41
+                cmd.append(email)
34ae41
 
34ae41
-        if user_type:
34ae41
-            cmd.append('--type')
34ae41
-            cmd.append(user_type)
34ae41
+            if password_file:
34ae41
+                cmd.append('--password-file')
34ae41
+                cmd.append(password_file)
34ae41
 
34ae41
-        if state:
34ae41
-            cmd.append('--state')
34ae41
-            cmd.append(state)
34ae41
+            if phone:
34ae41
+                cmd.append('--phone')
34ae41
+                cmd.append(phone)
34ae41
 
34ae41
-        if tps_profiles:
34ae41
-            cmd.append('--tps-profiles')
34ae41
-            cmd.append(','.join(tps_profiles))
34ae41
+            if user_type:
34ae41
+                cmd.append('--type')
34ae41
+                cmd.append(user_type)
34ae41
 
34ae41
-        if logger.isEnabledFor(logging.DEBUG):
34ae41
-            cmd.append('--debug')
34ae41
+            if state:
34ae41
+                cmd.append('--state')
34ae41
+                cmd.append(state)
34ae41
 
34ae41
-        elif logger.isEnabledFor(logging.INFO):
34ae41
-            cmd.append('--verbose')
34ae41
+            if tps_profiles:
34ae41
+                cmd.append('--tps-profiles')
34ae41
+                cmd.append(','.join(tps_profiles))
34ae41
 
34ae41
-        cmd.append(user_id)
34ae41
+            if logger.isEnabledFor(logging.DEBUG):
34ae41
+                cmd.append('--debug')
34ae41
 
34ae41
-        self.run(
34ae41
-            cmd,
34ae41
-            as_current_user=as_current_user,
34ae41
-            capture_output=True)
34ae41
+            elif logger.isEnabledFor(logging.INFO):
34ae41
+                cmd.append('--verbose')
34ae41
+
34ae41
+            cmd.append(user_id)
34ae41
+
34ae41
+            self.run(
34ae41
+                cmd,
34ae41
+                as_current_user=as_current_user,
34ae41
+                capture_output=True)
34ae41
+
34ae41
+        finally:
34ae41
+            shutil.rmtree(tmpdir)
34ae41
 
34ae41
     def modify_user(self, user_id, add_see_also=None, del_see_also=None,
34ae41
                     as_current_user=False):
34ae41
diff --git a/base/server/src/org/dogtagpki/server/cli/SubsystemUserAddCLI.java b/base/server/src/org/dogtagpki/server/cli/SubsystemUserAddCLI.java
34ae41
index 5a385c359f..04d68de758 100644
34ae41
--- a/base/server/src/org/dogtagpki/server/cli/SubsystemUserAddCLI.java
34ae41
+++ b/base/server/src/org/dogtagpki/server/cli/SubsystemUserAddCLI.java
34ae41
@@ -6,6 +6,8 @@
34ae41
 package org.dogtagpki.server.cli;
34ae41
 
34ae41
 import java.io.File;
34ae41
+import java.nio.file.Files;
34ae41
+import java.nio.file.Paths;
34ae41
 import java.util.Arrays;
34ae41
 import java.util.List;
34ae41
 
34ae41
@@ -60,6 +62,10 @@ public class SubsystemUserAddCLI extends CommandCLI {
34ae41
         option.setArgName("password");
34ae41
         options.addOption(option);
34ae41
 
34ae41
+        option = new Option(null, "password-file", true, "Password file");
34ae41
+        option.setArgName("path");
34ae41
+        options.addOption(option);
34ae41
+
34ae41
         option = new Option(null, "phone", true, "Phone");
34ae41
         option.setArgName("phone");
34ae41
         options.addOption(option);
34ae41
@@ -95,11 +101,16 @@ public class SubsystemUserAddCLI extends CommandCLI {
34ae41
 
34ae41
         String email = cmd.getOptionValue("email");
34ae41
         String password = cmd.getOptionValue("password");
34ae41
+        String passwordFile = cmd.getOptionValue("password-file");
34ae41
         String phone = cmd.getOptionValue("phone");
34ae41
         String type = cmd.getOptionValue("type");
34ae41
         String state = cmd.getOptionValue("state");
34ae41
         String tpsProfiles = cmd.getOptionValue("tps-profiles");
34ae41
 
34ae41
+        if (passwordFile != null) {
34ae41
+            password = new String(Files.readAllBytes(Paths.get(passwordFile)), "UTF-8").trim();
34ae41
+        }
34ae41
+
34ae41
         String catalinaBase = System.getProperty("catalina.base");
34ae41
 
34ae41
         TomcatJSS tomcatjss = TomcatJSS.getInstance();
34ae41
-- 
34ae41
2.30.2
34ae41