Gentoo Logo
Gentoo Spaceship




Note: Due to technical difficulties, the Archives are currently not up to date. GMANE provides an alternative service for most mailing lists.
c.f. bug 424647
List Archive: gentoo-osx
Navigation:
Lists: gentoo-osx: < Prev By Thread Next > < Prev By Date Next >
Headers:
To: gentoo-osx@g.o
From: Michael Haubenwallner <michael.haubenwallner@...>
Subject: Re: [PREFIX] User account management
Date: Wed, 14 Jun 2006 09:55:35 +0200
On Wed, 2006-06-14 at 09:37 +0200, Grobian wrote:
> On 14-06-2006 08:57:18 +0200, Michael Haubenwallner wrote:
> > On Tue, 2006-06-13 at 12:36 -0600, m h wrote:
> > > Michael-
> > > 
> > > Thanks for your response.  I'd like to have a look at your solution
> > > but didn't see any attachment.
> > 
> > Even not in the second mail I sent 4 minutes later, imo with
> > attachment ?
> 
> I've seen no second mail... maybe the mailing list software doesn't like
> attachments.  *sigh*

hmm, it was 'eutils.eclass.bz2' with ~15kB,
have extracted the relevant bits, trying as plaintext attachment...
-- 
Michael Haubenwallner                    SALOMON Automation GmbH
Forschung & Entwicklung                  A-8114 Friesach bei Graz
mailto:michael.haubenwallner@...  http://www.salomon.at
No HTML/MIME please, see http://expita.com/nomime.html
# Small wrapper for getent (Linux), nidump (Mac OS X),
# and pw (FreeBSD) used in enewuser()/enewgroup()
# Joe Jezak <josejx@...> and usata@g.o
# FBSD stuff: Aaron Walker <ka0ttic@g.o>
#
# egetent(database, key)
egetent() {
	local euser= euid= egroup= egid= eentry=
	while read line
	do
		case "$1:${line}" in
		'passwd:# enewuser:'*)
			eval $(echo "${line}" \
				| awk -F":" "{
					print \"euser='\"\$2\"'\";
					print \"euid='\"\$3\"'\";
					print \"eentry='\"\$2\":x:\"\$3\":\"\$4\":\"\$5\":\"\$6\":\"\$7\"'\";
				}")
			if [[ ${euser} = $2 ]] || [[ ${euid} = $2 ]]
			then
				echo "${eentry}"
				return 0
			fi
			;;
		'group:# enewgroup:'*)
			eval $(echo "${line}" \
				| awk -F":" "{
					print \"egroup='\"\$2\"'\";
					print \"egid='\"\$3\"'\";
					print \"eentry='\"\$2\":x:\"\$3\":'\";
				}")
			if [[ ${egroup} = $2 ]] || [[ ${egid} = $2 ]]
			then
				echo "${eentry}"
				return 0
			fi
			;;
		esac
	done <<-EOE
	$(cat "${ROOT}${AFFIX}"var/spool/emerge/doasroot)
	EOE
			
	if [[ "${USERLAND}" == "Darwin" ]] ; then
		case "$2" in
		  *[!0-9]*) # Non numeric
			nidump $1 . | awk -F":" "{ if (\$1 ~ /^$2$/) {print \$0;exit;} }"
			;;
		  *)	# Numeric
			nidump $1 . | awk -F":" "{ if (\$3 == $2) {print \$0;exit;} }"
			;;
		esac
	elif [[ "${USERLAND}" == "BSD" ]] ; then
		local action
		if [ "$1" == "passwd" ]
		then
			action="user"
		else
			action="group"
		fi
		pw show "${action}" "$2" -q
	else
		which nscd >& /dev/null && nscd -i "$1"
		getent "$1" "$2"
	fi
}

edoasroot() {
	if [[ ${ROOT} != / ]] || ( use secondary && [[ $(id -un) != root ]] )
	then
		touch "${ROOT}${AFFIX}"var/spool/emerge/doasroot
		[ -n "$1" ] && echo "$1" >> "${ROOT}${AFFIX}"var/spool/emerge/doasroot
		shift
		[ -n "$*" ] && echo "$(	for arg in "$@"
			do
				echo -n "'${arg}' "
			done
		)" >> "${ROOT}${AFFIX}"var/spool/emerge/doasroot
		return 0
	fi
	shift
	eval "$@"
}

# Simplify/standardize adding users to the system
# vapier@g.o
#
# enewuser(username, uid, shell, homedir, groups, extra options)
#
# Default values if you do not specify any:
# username:	REQUIRED !
# uid:		next available (see useradd(8))
#		note: pass -1 to get default behavior
# shell:	/bin/false
# homedir:	/dev/null
# groups:	none
# extra:	comment of 'added by portage for ${PN}'
enewuser() {
	# get the username
	local euser=$1; shift
	if [[ -z ${euser} ]] ; then
		eerror "No username specified !"
		die "Cannot call enewuser without a username"
	fi

	# lets see if the username already exists
	if [[ ${euser} == $(egetent passwd "${euser}" | cut -d: -f1) ]] ; then
		return 0
	fi
	einfo "Adding user '${euser}' to your system ..."

	# options to pass to useradd
	local opts=

	# handle uid
	local euid=$1; shift
	if [[ ! -z ${euid} ]] && [[ ${euid} != "-1" ]] ; then
		if [[ ${euid} -gt 0 ]] ; then
			if [[ ! -z $(egetent passwd ${euid}) ]] ; then
				euid="next"
			fi
		else
			eerror "Userid given but is not greater than 0 !"
			die "${euid} is not a valid UID"
		fi
	else
		euid="next"
	fi
	if [[ ${euid} == "next" ]] ; then
		local pwrange
		if [[ ${USERLAND} == "BSD" ]] ; then
			pwrange=$(jot 898 101)
		else
			pwrange=$(seq 101 999)
		fi
		for euid in ${pwrange} ; do
			[[ -z $(egetent passwd ${euid}) ]] && break
		done
	fi
	opts="${opts} -u ${euid}"
	einfo " - Userid: ${euid}"

	# handle shell
	local eshell=$1; shift
	if [[ ! -z ${eshell} ]] && [[ ${eshell} != "-1" ]] ; then
		if [[ ! -e ${eshell} ]] ; then
			eerror "A shell was specified but it does not exist !"
			die "${eshell} does not exist"
		fi
	else
		case ${USERLAND} in
			Darwin) eshell="/usr/bin/false";;
			BSD)    eshell="/usr/sbin/nologin";;
			*)      eshell="/bin/false";;
		esac
	fi
	einfo " - Shell: ${eshell}"
	opts="${opts} -s ${eshell}"

	# handle homedir
	local ehome=$1; shift
	if [[ -z ${ehome} ]] || [[ ${ehome} == "-1" ]] ; then
		ehome="/dev/null"
	fi
	einfo " - Home: ${ehome}"
	opts="${opts} -d ${ehome}"

	# handle groups
	local egroups=$1; shift
	local defgroup="" exgroups=""
	if [[ ! -z ${egroups} ]] ; then
		local oldifs=${IFS}

		export IFS=","
		for g in ${egroups} ; do
			export IFS=${oldifs}
			if [[ -z $(egetent group "${g}") ]] ; then
				eerror "You must add group ${g} to the system first"
				die "${g} is not a valid GID"
			fi
			if [[ -z ${defgroup} ]] ; then
				defgroup=${g}
			else
				exgroups="${exgroups},${g}"
			fi
			export IFS=","
		done
		export IFS=${oldifs}

		opts="${opts} -g ${defgroup}"
		if [[ ! -z ${exgroups} ]] ; then
			opts="${opts} -G ${exgroups:1}"
		fi
	else
		egroups="(none)"
	fi
	einfo " - Groups: ${egroups}"

	# handle extra and add the user
	local oldsandbox=${SANDBOX_ON}
	export SANDBOX_ON="0"
	edoasroot "# enewuser:${euser}:${euid}:${defgroup}:added by portage for ${PN}:${ehome}:${eshell}"
	case ${USERLAND} in
	Darwin)
		### Make the user
		if [[ -z $@ ]] ; then
			edoasroot '' dscl . create /users/${euser} uid ${euid}
			edoasroot '' dscl . create /users/${euser} shell ${eshell}
			edoasroot '' dscl . create /users/${euser} home ${ehome}
			edoasroot '' dscl . create /users/${euser} realname "added by portage for ${PN}"
			### Add the user to the groups specified
			local oldifs=${IFS}
			export IFS=","
			for g in ${egroups} ; do
				edoasroot '' dscl . merge /groups/${g} users ${euser}
			done
			export IFS=${oldifs}
		else
			einfo "Extra options are not supported on Darwin yet"
			einfo "Please report the ebuild along with the info below"
			einfo "eextra: $@"
			die "Required function missing"
		fi
		;;
	BSD)
		if [[ -z $@ ]] ; then
			edoasroot '' pw useradd ${euser} ${opts} \
				-c "added by portage for ${PN}" \
				die "enewuser failed"
		else
			einfo " - Extra: $@"
			edoasroot '' pw useradd ${euser} ${opts} \
				"$@" || die "enewuser failed"
		fi
		;;
	*)
		if [[ -z $@ ]] ; then
			edoasroot '' useradd ${opts} ${euser} \
				-c "added by portage for ${PN}" \
				|| die "enewuser failed"
		else
			einfo " - Extra: $@"
			edoasroot '' useradd ${opts} ${euser} "$@" \
				|| die "enewuser failed"
		fi
		;;
	esac
	export SANDBOX_ON=${oldsandbox}

	if [ ! -e "${ehome}" ] && [ ! -e "${D}/${ehome}" ]
	then
		einfo " - Creating ${ehome} in ${D}"
		dodir ${ehome}
		edoasroot '' fowners ${euser} ${ehome}
		edoasroot '' fperms 755 ${ehome}
	fi
}

# Simplify/standardize adding groups to the system
# vapier@g.o
#
# enewgroup(group, gid)
#
# Default values if you do not specify any:
# groupname:	REQUIRED !
# gid:		next available (see groupadd(8))
# extra:	none
enewgroup() {
	# get the group
	local egroup="$1"; shift
	if [ -z "${egroup}" ]
	then
		eerror "No group specified !"
		die "Cannot call enewgroup without a group"
	fi

	# see if group already exists
	if [ "${egroup}" == "`egetent group \"${egroup}\" | cut -d: -f1`" ]
	then
		return 0
	fi
	einfo "Adding group '${egroup}' to your system ..."

	# options to pass to useradd
	local opts=

	# handle gid
	local egid="$1"; shift
	if [ ! -z "${egid}" ]
	then
		if [ "${egid}" -gt 0 ]
		then
			if [ -z "`egetent group ${egid}`" ]
			then
				if [[ "${USERLAND}" == "Darwin" ]]; then
					opts="${opts} ${egid}"
				else
					opts="${opts} -g ${egid}"
				fi
			else
				egid="next available; requested gid taken"
			fi
		else
			eerror "Groupid given but is not greater than 0 !"
			die "${egid} is not a valid GID"
		fi
	else
		egid="next available"
	fi
	einfo " - Groupid: ${egid}"

	# handle extra
	local eextra="$@"
	opts="${opts} ${eextra}"

	# add the group
	local oldsandbox="${SANDBOX_ON}"
	export SANDBOX_ON="0"
	edoasroot "# enewgroup:${egroup}:${egid}"
	if [[ "${USERLAND}" == "Darwin" ]]; then
		if [ ! -z "${eextra}" ];
		then
			einfo "Extra options are not supported on Darwin/OS X yet"
			einfo "Please report the ebuild along with the info below"
			einfo "eextra: ${eextra}"
			die "Required function missing"
		fi

		# If we need the next available
		case ${egid} in
		  *[!0-9]*) # Non numeric
			for egid in `jot 898 101`; do
				[ -z "`egetent group ${egid}`" ] && break
			done
		esac
		edoasroot '' dscl . create /groups/${egroup} gid ${egid}
		edoasroot '' dscl . create /groups/${egroup} passwd '*'
	elif [[ "${USERLAND}" == "BSD" ]] ; then
		case ${egid} in
			*[!0-9]*) # Non numeric
				for egid in `jot 898 101`; do
					[ -z "`egetent group ${egid}`" ] && break
				done
		esac
		edoasroot '' pw groupadd ${egroup} -g ${egid} || die "enewgroup failed"
	else
		edoasroot '' groupadd ${opts} ${egroup} || die "enewgroup failed"
	fi
	export SANDBOX_ON="${oldsandbox}"
}

References:
[PREFIX] User account management
-- m h
Re: [PREFIX] User account management
-- Grobian
Re: [PREFIX] User account management
-- Michael Haubenwallner
Re: [PREFIX] User account management
-- m h
Re: [PREFIX] User account management
-- Michael Haubenwallner
Re: [PREFIX] User account management
-- Grobian
Navigation:
Lists: gentoo-osx: < Prev By Thread Next > < Prev By Date Next >
Previous by thread:
Re: [PREFIX] User account management
Next by thread:
PREFIX on Cygwin
Previous by date:
Re: [PREFIX] User account management
Next by date:
Re: PREFIX on Cygwin


Updated Jun 17, 2009

Summary: Archive of the gentoo-osx mailing list.

Donate to support our development efforts.

Copyright 2001-2013 Gentoo Foundation, Inc. Questions, Comments? Contact us.