2024-05-17 23:04:34 +08:00
|
|
|
from logging import getLogger
|
|
|
|
from builder.build.filesystem import chroot_run
|
|
|
|
from builder.lib.config import ArchBuilderConfigError
|
|
|
|
from builder.lib.context import ArchBuilderContext
|
|
|
|
log = getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def proc_user(ctx: ArchBuilderContext, cfg: dict):
|
2024-05-20 09:56:42 +08:00
|
|
|
"""
|
|
|
|
Create a new user and set password
|
|
|
|
"""
|
2024-05-17 23:04:34 +08:00
|
|
|
if "name" not in cfg: raise ArchBuilderConfigError("username not set")
|
|
|
|
name = cfg["name"]
|
|
|
|
cmds = []
|
|
|
|
if ctx.passwd.lookup_name(name) is None:
|
2024-05-20 09:56:42 +08:00
|
|
|
# user is not exists, create it
|
2024-05-17 23:04:34 +08:00
|
|
|
cmds.append("useradd")
|
2024-05-20 09:56:42 +08:00
|
|
|
cmds.append("-m") # create home
|
2024-05-17 23:04:34 +08:00
|
|
|
action = "created"
|
|
|
|
else:
|
2024-05-20 09:56:42 +08:00
|
|
|
# user is already exists, modify it
|
2024-05-17 23:04:34 +08:00
|
|
|
cmds.append("usermod")
|
|
|
|
action = "modified"
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# add all options
|
2024-05-17 23:04:34 +08:00
|
|
|
if "uid" in cfg: cmds.extend(["-u", str(cfg["uid"])])
|
|
|
|
if "gid" in cfg: cmds.extend(["-g", str(cfg["gid"])])
|
|
|
|
if "home" in cfg: cmds.extend(["-d", cfg["home"]])
|
|
|
|
if "shell" in cfg: cmds.extend(["-s", cfg["shell"]])
|
|
|
|
if "groups" in cfg: cmds.extend(["-G", str(cfg["groups"])])
|
|
|
|
cmds.append(name)
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# run useradd or usermod
|
2024-05-17 23:04:34 +08:00
|
|
|
ret = chroot_run(ctx, cmds)
|
|
|
|
if ret != 0: raise OSError(f"{cmds[0]} failed")
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# we want to set a password for user
|
2024-05-17 23:04:34 +08:00
|
|
|
if "password" in cfg:
|
|
|
|
cmds = ["chpasswd"]
|
|
|
|
text = f"{name}:{cfg['password']}\n"
|
|
|
|
ret = chroot_run(ctx, cmds, stdin=text)
|
|
|
|
if ret != 0: raise OSError("chpasswd failed")
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# reload user database
|
2024-05-17 23:04:34 +08:00
|
|
|
ctx.reload_passwd()
|
|
|
|
log.info(f"{action} user {name}")
|
|
|
|
|
|
|
|
|
|
|
|
def proc_group(ctx: ArchBuilderContext, cfg: dict):
|
2024-05-20 09:56:42 +08:00
|
|
|
"""
|
|
|
|
Create a new group
|
|
|
|
"""
|
2024-05-17 23:04:34 +08:00
|
|
|
if "name" not in cfg: raise ArchBuilderConfigError("groupname not set")
|
|
|
|
name = cfg["name"]
|
|
|
|
cmds = []
|
|
|
|
if ctx.passwd.lookup_name(name) is None:
|
2024-05-20 09:56:42 +08:00
|
|
|
# group is not exists, create it
|
2024-05-17 23:04:34 +08:00
|
|
|
cmds.append("groupadd")
|
|
|
|
action = "created"
|
|
|
|
else:
|
2024-05-20 09:56:42 +08:00
|
|
|
# group is already exists, modify it
|
2024-05-17 23:04:34 +08:00
|
|
|
cmds.append("groupmod")
|
|
|
|
action = "modified"
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# add all options
|
2024-05-17 23:04:34 +08:00
|
|
|
if "gid" in cfg: cmds.extend(["-g", str(cfg["gid"])])
|
|
|
|
cmds.append(name)
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# run groupadd or groupmod
|
2024-05-17 23:04:34 +08:00
|
|
|
ret = chroot_run(ctx, cmds)
|
|
|
|
if ret != 0: raise OSError(f"{name} failed")
|
2024-05-20 09:56:42 +08:00
|
|
|
|
|
|
|
# reload user database
|
2024-05-17 23:04:34 +08:00
|
|
|
ctx.reload_passwd()
|
|
|
|
log.info(f"{action} group {name}")
|
|
|
|
|
|
|
|
|
|
|
|
def proc_users(ctx: ArchBuilderContext):
|
2024-05-20 09:56:42 +08:00
|
|
|
"""
|
|
|
|
Create all users
|
|
|
|
"""
|
2024-05-17 23:04:34 +08:00
|
|
|
for user in ctx.get("sysconf.user", []):
|
|
|
|
proc_user(ctx, user)
|
|
|
|
|
|
|
|
|
|
|
|
def proc_groups(ctx: ArchBuilderContext):
|
2024-05-20 09:56:42 +08:00
|
|
|
"""
|
|
|
|
Create all groups
|
|
|
|
"""
|
2024-05-17 23:04:34 +08:00
|
|
|
for group in ctx.get("sysconf.group", []):
|
|
|
|
proc_group(ctx, group)
|
|
|
|
|
|
|
|
|
|
|
|
def proc_usergroup(ctx: ArchBuilderContext):
|
2024-05-20 09:56:42 +08:00
|
|
|
"""
|
|
|
|
Create all users and groups
|
|
|
|
"""
|
|
|
|
proc_groups(ctx) # create groups before users
|
2024-05-17 23:04:34 +08:00
|
|
|
proc_users(ctx)
|