public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Michał Górny" <mgorny@gentoo.org>
To: gentoo-dev@lists.gentoo.org
Cc: "Michał Górny" <mgorny@gentoo.org>
Subject: [gentoo-dev] [PATCH v4 13/19] user.eclass: Introduce e{get,set}groups
Date: Tue, 11 Jun 2019 18:23:41 +0200	[thread overview]
Message-ID: <20190611162347.2989-14-mgorny@gentoo.org> (raw)
In-Reply-To: <20190611162347.2989-1-mgorny@gentoo.org>

---
 eclass/user.eclass | 88 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)

diff --git a/eclass/user.eclass b/eclass/user.eclass
index 0e7aa43d8932..fdf98caa6099 100644
--- a/eclass/user.eclass
+++ b/eclass/user.eclass
@@ -434,6 +434,24 @@ egetcomment() {
 	egetent passwd "$1" | cut -d: -f${pos}
 }
 
+# @FUNCTION: egetgroups
+# @USAGE: <user>
+# @DESCRIPTION:
+# Gets all the groups user belongs to.  The primary group is returned
+# first, then all supplementary groups.  Groups are ','-separated.
+egetgroups() {
+	[[ $# -eq 1 ]] || die "usage: egetgroups <user>"
+
+	local egroups_arr
+	read -r -a egroups_arr < <(id -G -n "$1")
+
+	local defgroup=${egroups_arr[0]}
+	# sort supplementary groups to make comparison possible
+	readarray -t exgroups_arr < <(printf '%s\n' "${egroups_arr[@]:1}" | sort)
+	local exgroups=${exgroups_arr[*]}
+	echo "${defgroup}${exgroups:+,${exgroups// /,}}"
+}
+
 # @FUNCTION: esethome
 # @USAGE: <user> <homedir>
 # @DESCRIPTION:
@@ -623,4 +641,74 @@ esetcomment() {
 	esac
 }
 
+# @FUNCTION: esetgroups
+# @USAGE: <user> <groups>
+# @DESCRIPTION:
+# Update the group field in a platform-agnostic way.
+# Required parameters is the username and the new list of groups,
+# primary group first.
+esetgroups() {
+	_assert_pkg_ebuild_phase ${FUNCNAME}
+
+	[[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <user> <groups>"
+
+	# get the username
+	local euser=$1; shift
+
+	# lets see if the username already exists
+	if [[ -z $(egetent passwd "${euser}") ]] ; then
+		ewarn "User does not exist, cannot set group -- skipping."
+		return 1
+	fi
+
+	# handle group
+	local egroups=$1; shift
+
+	local g egroups_arr=()
+	IFS="," read -r -a egroups_arr <<<"${egroups}"
+	[[ ${#egroups_arr[@]} -gt 0 ]] || die "${FUNCNAME}: no groups specified"
+
+	for g in "${egroups_arr[@]}" ; do
+		if [[ -z $(egetent group "${g}") ]] ; then
+			eerror "You must add group ${g} to the system first"
+			die "${g} is not a valid GID"
+		fi
+	done
+
+	local defgroup=${egroups_arr[0]} exgroups_arr=()
+	# sort supplementary groups to make comparison possible
+	readarray -t exgroups_arr < <(printf '%s\n' "${egroups_arr[@]:1}" | sort)
+	local exgroups=${exgroups_arr[*]}
+	exgroups=${exgroups// /,}
+	egroups=${defgroup}${exgroups:+,${exgroups}}
+
+	# exit with no message if group membership is up to date
+	if [[ $(egetgroups "${euser}") == ${egroups} ]]; then
+		return 0
+	fi
+
+	local opts=( -g "${defgroup}" -G "${exgroups}" )
+	einfo "Updating groups for user '${euser}' ..."
+	einfo " - Groups: ${egroups}"
+
+	# update the group
+	case ${CHOST} in
+	*-freebsd*|*-dragonfly*)
+		pw usermod "${euser}" "${opts[@]}" && return 0
+		[[ $? == 8 ]] && eerror "${euser} is in use, cannot update groups"
+		eerror "There was an error when attempting to update the groups for ${euser}"
+		eerror "Please update it manually on your system:"
+		eerror "\t pw usermod \"${euser}\" ${opts[*]}"
+		;;
+
+	*)
+		usermod "${opts[@]}" "${euser}" && return 0
+		[[ $? == 8 ]] && eerror "${euser} is in use, cannot update groups"
+		eerror "There was an error when attempting to update the groups for ${euser}"
+		eerror "Please update it manually on your system (as root):"
+		eerror "\t usermod ${opts[*]} \"${euser}\""
+		;;
+	esac
+}
+
 fi
-- 
2.22.0



  parent reply	other threads:[~2019-06-11 16:28 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-11 16:23 [gentoo-dev] [PATCH v4 00/19] User/group packages Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 01/19] user.eclass: Remove dead/broken Darwin support Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 02/19] user.eclass: NetBSD has 'getent' Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 03/19] user.eclass: Do not create user-group automatically Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 04/19] user.eclass: Prevent automated home creation in useradd Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 05/19] user.eclass: Support disabling home directory creation Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 06/19] user.eclass: Support forcing specified UID/GID Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 07/19] user.eclass: Die if no free UID/GID is found Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 08/19] user.eclass: Factor out finding nologin into separate function Michał Górny
2019-06-13  1:11   ` Michael Orlitzky
2019-06-13  5:33     ` Michał Górny
2019-06-13 13:01       ` Michael Orlitzky
2019-06-13 13:18         ` Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 09/19] user.eclass: Introduce esetshell Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 10/19] user.eclass: Introduce eget{user,group}name Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 11/19] user.eclass: Also permit using functions in pkg_*rm phases Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 12/19] user.eclass: Support getting & setting comment field Michał Górny
2019-06-12  7:08   ` Jaco Kroon
2019-06-11 16:23 ` Michał Górny [this message]
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 14/19] acct-group.eclass: A new eclass to maintain group accounts Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 15/19] acct-user.eclass: A new eclass to maintain user accounts Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 16/19] acct-user.eclass: Supporting locking & unlocking accounts Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 17/19] acct-group/ftp: Add 'ftp' group (GID 21) Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 18/19] acct-user/ftp: Add 'ftp' user (UID 21) Michał Górny
2019-06-11 16:23 ` [gentoo-dev] [PATCH v4 19/19] net-ftp/ftpbase: Utilize {group,user}/ftp Michał Górny
2019-06-13  1:15   ` Michael Orlitzky
2019-06-13  8:54 ` [gentoo-dev] [PATCH v4 00/19] User/group packages Alexey Shvetsov
2019-06-13 12:58   ` Michael Orlitzky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190611162347.2989-14-mgorny@gentoo.org \
    --to=mgorny@gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox