From 04d3c0fc1724a1030e7d6536f9cfc77c57556541 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Mon, 1 Jun 2020 20:33:15 +0200 Subject: [PATCH] stages/users: fix conditionals for zero & "" strs Support setting uids, gids with values of `0` as well as passwords and descriptions with the empty string, by explicitly checking the value of each against `None`, because simple `if` conditionals are false for those. --- stages/org.osbuild.users | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/stages/org.osbuild.users b/stages/org.osbuild.users index 9020d7fd..88f9a94e 100755 --- a/stages/org.osbuild.users +++ b/stages/org.osbuild.users @@ -82,19 +82,19 @@ def getpwnam(root, name): def useradd(root, name, uid=None, gid=None, groups=None, description=None, home=None, shell=None, password=None): arguments = [] - if uid: - arguments += ["--uid", uid] - if gid: - arguments += ["--gid", gid] + if uid is not None: + arguments += ["--uid", str(uid)] + if gid is not None: + arguments += ["--gid", str(gid)] if groups: arguments += ["--groups", ",".join(groups)] - if description: + if description is not None: arguments += ["--comment", description] if home: arguments += ["--home-dir", home] if shell: arguments += ["--shell", shell] - if password: + if password is not None: arguments += ["--password", password] subprocess.run(["chroot", root, "useradd", *arguments, name], check=True) @@ -102,17 +102,17 @@ def useradd(root, name, uid=None, gid=None, groups=None, description=None, home= def usermod(root, name, gid=None, groups=None, description=None, home=None, shell=None, password=None): arguments = [] - if gid: + if gid is not None: arguments += ["--gid", gid] if groups: arguments += ["--groups", ",".join(groups)] - if description: + if description is not None: arguments += ["--comment", description] if home: arguments += ["--home", home] if shell: arguments += ["--shell", shell] - if password: + if password is not None: arguments += ["--password", password] if arguments: @@ -148,8 +148,8 @@ def main(tree, options): password = user_options.get("password") passwd = getpwnam(tree, name) - if passwd: - if uid: + if passwd is not None: + if uid is not None: print(f"Error: can't set uid of existing user '{name}'") return 1 usermod(tree, name, gid, groups, description, home, shell, password)