]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Ensure that username and uid are consistent
authorJoerg Behrmann <behrmann@physik.fu-berlin.de>
Tue, 25 Apr 2023 16:53:57 +0000 (18:53 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 26 Apr 2023 07:33:40 +0000 (09:33 +0200)
While SUDO_UID and SUDO_GID are useful, since sudo allows to run things as
different UID and GID, SUDO_USER is less useful, since it can't change the
mapping from UID to name in the namespace it's in. Let's cut out the middleman
of getpass.getuser, that looks through several more environment
variables (LOGNAME, USER, LNAME, USERNAME) before falling back to os.getuid and
looking up the corresponding name and do that right away.

mkosi/util.py

index e674079b6191d54e8817c63bad0ee1c9359eb64e..526cbe953ce4ab5c4f48707a6dec71befa1e1928 100644 (file)
@@ -245,9 +245,7 @@ class InvokingUser:
 
     @classmethod
     def name(cls) -> str:
-        if uid := cls._uid_from_env():
-            return pwd.getpwuid(uid).pw_name
-        return getpass.getuser()
+        return pwd.getpwuid(cls.uid()).pw_name
 
     @classmethod
     def home(cls) -> Path: