[elbe-devel] [PATCH v1 07/15] xmlpreprocess: add password processing for root

Holger Dengler holger at hdengler.de
Wed Jun 8 22:39:50 CEST 2022


Create or update the hashed passwords for root. Only METHOD_SHA512 ($6)
is currently supported.

Signed-off-by: Holger Dengler <holger at hdengler.de>
---
 elbepack/xmlpreprocess.py | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/elbepack/xmlpreprocess.py b/elbepack/xmlpreprocess.py
index f3c2f2a7f..69642bc51 100644
--- a/elbepack/xmlpreprocess.py
+++ b/elbepack/xmlpreprocess.py
@@ -14,6 +14,7 @@ from optparse import OptionGroup
 from itertools import islice
 from urllib.error import HTTPError,URLError
 from urllib.request import urlopen
+from crypt import crypt, METHOD_SHA512
 
 from lxml import etree
 from lxml.etree import XMLParser, parse, Element
@@ -251,6 +252,33 @@ def preprocess_mirrors(xml):
             option.text = opt
             options.append(option)
 
+def preprocess_root_password(xml):
+    """Preprocess the plain-text password for root. A given plain-text
+    password will be hashed and added to the XML.
+    The plain-text password node will be removed (unless otherwise
+    specified).
+    """
+
+    parent = xml.find('.//target')
+    if parent is None:
+        return
+
+    passwd = parent.find('passwd')
+    if passwd is None:
+        return
+
+    passwd_hashed = parent.find('passwd_hashed')
+    if passwd_hashed is None:
+        # if required, add mandatory passwd_hashed node
+        passwd_hashed = etree.Element('passwd_hashed')
+        parent.insert(list(parent).index(passwd) + 1,
+                      passwd_hashed)
+
+    # update hashed password node
+    passwd_hashed.text = '%s' % crypt(passwd.text, METHOD_SHA512)
+    print('[INFO] update hashed password for root.')
+
+
 def xmlpreprocess(fname, output, variants=None, proxy=None):
 
     # pylint: disable=too-many-locals
@@ -334,6 +362,8 @@ def xmlpreprocess(fname, output, variants=None, proxy=None):
 
         preprocess_mirrors(xml)
 
+        preprocess_root_password(xml)
+
         if schema.validate(xml):
             # if validation succedes write xml file
             xml.write(
-- 
2.36.1



More information about the elbe-devel mailing list