user-info: import from Gentoo & adjust to CrOS APIs

We use egetent from our user.eclass since it handles our account db,
and it has a compatible API.  All other functions are imported as-is
except for egetgroups which is disabled.  Nothing in Gentoo uses it,
and the Gentoo implementation doesn't work well with our setup.

The egethome & egetshell funcs are duplicated between the user eclasses
for now, but since they're implemented the same, it isn't a big deal.
We have to clean up some ebuilds in the tree before we can delete from
our user.eclass.

BUG=b:187790077
TEST=CQ passes

Change-Id: If97d81d523b5b53aae752055e1e487b8a5805456
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/overlays/eclass-overlay/+/4111961
Reviewed-by: Ram Chandrasekar <rchandrasekar@google.com>
Commit-Queue: Ram Chandrasekar <rchandrasekar@google.com>
Commit-Queue: Mike Frysinger <vapier@chromium.org>
Auto-Submit: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/eclass/user-info.eclass b/eclass/user-info.eclass
new file mode 100644
index 0000000..d6fc4fe
--- /dev/null
+++ b/eclass/user-info.eclass
@@ -0,0 +1,115 @@
+# Copyright 1999-2022 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+# @ECLASS: user-info.eclass
+# @MAINTAINER:
+# base-system@gentoo.org (Linux)
+# MichaƂ Górny <mgorny@gentoo.org> (NetBSD)
+# @SUPPORTED_EAPIS: 6 7 8
+# @BLURB: Read-only access to user and group information
+
+case ${EAPI} in
+	6|7|8) ;;
+	*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
+esac
+
+if [[ -z ${_USER_INFO_ECLASS} ]]; then
+_USER_INFO_ECLASS=1
+
+# TODO(b/187790077): Move user eclass APIs here that align w/Gentoo.
+inherit user
+
+# @FUNCTION: egetusername
+# @USAGE: <uid>
+# @DESCRIPTION:
+# Gets the username for given UID.
+egetusername() {
+	[[ $# -eq 1 ]] || die "usage: egetusername <uid>"
+
+	egetent passwd "$1" | cut -d: -f1
+}
+
+# @FUNCTION: egetgroupname
+# @USAGE: <gid>
+# @DESCRIPTION:
+# Gets the group name for given GID.
+egetgroupname() {
+	[[ $# -eq 1 ]] || die "usage: egetgroupname <gid>"
+
+	egetent group "$1" | cut -d: -f1
+}
+
+# @FUNCTION: egethome
+# @USAGE: <user>
+# @DESCRIPTION:
+# Gets the home directory for the specified user.
+egethome() {
+	local pos
+
+	[[ $# -eq 1 ]] || die "usage: egethome <user>"
+
+	case ${CHOST} in
+	*-freebsd*|*-dragonfly*)
+		pos=9
+		;;
+	*)	# Linux, NetBSD, OpenBSD, etc...
+		pos=6
+		;;
+	esac
+
+	egetent passwd "$1" | cut -d: -f${pos}
+}
+
+# @FUNCTION: egetshell
+# @USAGE: <user>
+# @DESCRIPTION:
+# Gets the shell for the specified user.
+egetshell() {
+	local pos
+
+	[[ $# -eq 1 ]] || die "usage: egetshell <user>"
+
+	case ${CHOST} in
+	*-freebsd*|*-dragonfly*)
+		pos=10
+		;;
+	*)	# Linux, NetBSD, OpenBSD, etc...
+		pos=7
+		;;
+	esac
+
+	egetent passwd "$1" | cut -d: -f${pos}
+}
+
+# @FUNCTION: egetcomment
+# @USAGE: <user>
+# @DESCRIPTION:
+# Gets the comment field for the specified user.
+egetcomment() {
+	local pos
+
+	[[ $# -eq 1 ]] || die "usage: egetcomment <user>"
+
+	case ${CHOST} in
+	*-freebsd*|*-dragonfly*)
+		pos=8
+		;;
+	*)	# Linux, NetBSD, OpenBSD, etc...
+		pos=5
+		;;
+	esac
+
+	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() {
+	# Nothing uses this, so not clear we care.
+	die "Not implemented: please contact go/cros-build-help"
+}
+
+fi
diff --git a/eclass/user.eclass b/eclass/user.eclass
index 0837b1f..5ded7e3 100644
--- a/eclass/user.eclass
+++ b/eclass/user.eclass
@@ -635,6 +635,7 @@
 	einfo "Done with group: '${egroup}'."
 }
 
+# TODO(b/187790077): Delete this once ebuilds move to user-info.eclass.
 # @FUNCTION: egethome
 # @USAGE: <user>
 # @DESCRIPTION:
@@ -644,6 +645,7 @@
 	egetent passwd "$1" | cut -d: -f6
 }
 
+# TODO(b/187790077): Delete this once ebuilds move to user-info.eclass.
 # @FUNCTION: egetshell
 # @USAGE: <user>
 # @DESCRIPTION: