From eead5c05b0ab78e3b7ed8271481af13189bb7014 Mon Sep 17 00:00:00 2001 From: Joey Riches Date: Tue, 28 May 2024 22:48:22 +0100 Subject: [PATCH] commands: Add AddUserSubGroupRange and AddUserSubUserRange To write /etc/sub{g,u}id files. --- commands/main.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/commands/main.go b/commands/main.go index 82ce948..456ab67 100644 --- a/commands/main.go +++ b/commands/main.go @@ -108,6 +108,26 @@ func AddGroup(root, groupName string, groupID int) error { return ChrootExec(root, cmd) } +// AddUserSubUserRange will write /etc/subuid with a subordinate UID range for a user +func AddUserSubUserRange(root, userName string, rangeMin, rangeMax int) error { + + hack := fmt.Sprintf("touch /etc/subuid") + ChrootExec(root, hack) + + cmd := fmt.Sprintf("/usr/sbin/usermod --add-subuids %d-%d \"%s\"", rangeMin, rangeMax, userName) + return ChrootExec(root, cmd) +} + +// AddUserSubUserRange will write /etc/subgid with a subordinate GID range for a user +func AddUserSubGroupRange(root, userName string, rangeMin, rangeMax int) error { + + hack := fmt.Sprintf("touch /etc/subgid") + ChrootExec(root, hack) + + cmd := fmt.Sprintf("/usr/sbin/usermod --add-subgids %d-%d \"%s\"", rangeMin, rangeMax, userName) + return ChrootExec(root, cmd) +} + // AddUser will chroot into the given root and add a user func AddUser(root, userName, gecos, home, shell string, uid, gid int) error { cmd := fmt.Sprintf("/usr/sbin/useradd -m -d \"%s\" -s \"%s\" -u %d -g %d \"%s\" -c \"%s\"",