From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 08258138334 for ; Sun, 9 Jun 2019 11:32:49 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 11226E097A; Sun, 9 Jun 2019 11:28:40 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 93C6DE0975 for ; Sun, 9 Jun 2019 11:28:39 +0000 (UTC) Received: from localhost.localdomain (d202-252.icpnet.pl [109.173.202.252]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: mgorny) by smtp.gentoo.org (Postfix) with ESMTPSA id 18D7C345ABD; Sun, 9 Jun 2019 11:28:37 +0000 (UTC) From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= To: gentoo-dev@lists.gentoo.org Cc: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Subject: [gentoo-dev] [PATCH v3 12/19] user.eclass: Support getting & setting comment field Date: Sun, 9 Jun 2019 13:28:07 +0200 Message-Id: <20190609112814.15907-13-mgorny@gentoo.org> X-Mailer: git-send-email 2.22.0.rc3 In-Reply-To: <20190609112814.15907-1-mgorny@gentoo.org> References: <20190609112814.15907-1-mgorny@gentoo.org> Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Archives-Salt: 977b6aaa-ccb0-42b0-9d27-279164fa35f3 X-Archives-Hash: 817981e2d893477b034a9af56473e80f Signed-off-by: Michał Górny --- eclass/user.eclass | 77 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/eclass/user.eclass b/eclass/user.eclass index ceb805675cf6..f4f6c75f3b71 100644 --- a/eclass/user.eclass +++ b/eclass/user.eclass @@ -417,6 +417,27 @@ egetshell() { egetent passwd "$1" | cut -d: -f${pos} } +# @FUNCTION: egetcomment +# @USAGE: +# @DESCRIPTION: +# Gets the comment field for the specified user. +egetcomment() { + local pos + + [[ $# -eq 1 ]] || die "usage: egetshell " + + case ${CHOST} in + *-freebsd*|*-dragonfly*) + pos=8 + ;; + *) # Linux, NetBSD, OpenBSD, etc... + pos=5 + ;; + esac + + egetent passwd "$1" | cut -d: -f${pos} +} + # @FUNCTION: esethome # @USAGE: # @DESCRIPTION: @@ -550,4 +571,60 @@ esetshell() { esac } +# @FUNCTION: esetcomment +# @USAGE: +# @DESCRIPTION: +# Update the comment field in a platform-agnostic way. +# Required parameters is the username and the new comment. +esetcomment() { + _assert_pkg_ebuild_phase ${FUNCNAME} + + # get the username + local euser=$1; shift + if [[ -z ${euser} ]] ; then + eerror "No username specified !" + die "Cannot call esetcomment without a username" + fi + + # lets see if the username already exists + if [[ -z $(egetent passwd "${euser}") ]] ; then + ewarn "User does not exist, cannot set comment -- skipping." + return 1 + fi + + # handle comment + local ecomment=$1; shift + if [[ -z ${ecomment} ]] ; then + eerror "No comment specified !" + die "Cannot call esetcomment without a comment" + fi + + # exit with no message if comment is up to date + if [[ $(egetcomment "${euser}") == ${ecomment} ]]; then + return 0 + fi + + einfo "Updating comment for user '${euser}' ..." + einfo " - Comment: ${ecomment}" + + # update the comment + case ${CHOST} in + *-freebsd*|*-dragonfly*) + pw usermod "${euser}" -c "${ecomment}" && return 0 + [[ $? == 8 ]] && eerror "${euser} is in use, cannot update comment" + eerror "There was an error when attempting to update the comment for ${euser}" + eerror "Please update it manually on your system:" + eerror "\t pw usermod \"${euser}\" -c \"${ecomment}\"" + ;; + + *) + usermod -c "${ecomment}" "${euser}" && return 0 + [[ $? == 8 ]] && eerror "${euser} is in use, cannot update comment" + eerror "There was an error when attempting to update the comment for ${euser}" + eerror "Please update it manually on your system (as root):" + eerror "\t usermod -c \"${ecomment}\" \"${euser}\"" + ;; + esac +} + fi -- 2.22.0.rc3