Path: blob/main/Mk/Scripts/do-users-groups.sh
16461 views
#!/bin/sh1#2# MAINTAINER: [email protected]34set -e5set -o pipefail67. "${dp_SCRIPTSDIR}/functions.sh"89validate_env dp_ECHO_MSG dp_GID_FILES dp_GID_OFFSET dp_GROUPS_BLACKLIST \10dp_INSTALL dp_OPSYS dp_OSVERSION dp_PREFIX dp_PW dp_SCRIPTSDIR \11dp_UG_DEINSTALL dp_UG_INSTALL dp_UID_FILES dp_UID_OFFSET \12dp_USERS_BLACKLIST1314[ -n "${DEBUG_MK_SCRIPTS}" -o -n "${DEBUG_MK_SCRIPTS_DO_USERS_GROUPS}" ] && set -x1516set -u1718USERS=$119GROUPS=$22021error() {22${dp_ECHO_MSG} "${1}"2324exit 125}2627# Lines from GID and UID files both contain *. As we do not need any pathname28# expansion, disable globbing.29set -f3031rm -f "${dp_UG_INSTALL}" "${dp_UG_DEINSTALL}" || :3233if [ "${dp_OPSYS}" = FreeBSD ] ; then34cat >> "${dp_UG_INSTALL}" <<-eot35if [ -n "\${PKG_ROOTDIR}" ] && [ "\${PKG_ROOTDIR}" != "/" ]; then36PW="${dp_PW} -R \${PKG_ROOTDIR}"37else38PW=${dp_PW}39fi40eot41else42echo "PW=${dp_PW}" >> "${dp_UG_INSTALL}"43fi4445# Both scripts need to start the same, so46cp -f "${dp_UG_INSTALL}" "${dp_UG_DEINSTALL}"4748if [ -n "${GROUPS}" ]; then49for file in ${dp_GID_FILES}; do50if [ ! -f "${file}" ]; then51error "** ${file} doesn't exist. Exiting."52fi53done54${dp_ECHO_MSG} "===> Creating groups"55echo "echo \"===> Creating groups\"" >> "${dp_UG_INSTALL}"56for group in ${GROUPS}; do57# _bgpd:*:130:58if ! grep -q "^${group}:" ${dp_GID_FILES}; then \59error "** Cannot find any information about group \`${group}' in ${dp_GID_FILES}"60fi61while read -r line; do62# Do not change IFS for more than one command, if we63# changed IFS around the while read, it would mess up64# the string splitting in the heredoc command.65o_IFS=${IFS}66IFS=":"67set -- ${line}68IFS=${o_IFS}69group=$170gid=$371if [ -z "${gid}" ]; then72error "Group line for group ${group} has no gid"73fi74gid=$((gid+dp_GID_OFFSET))75cat >> "${dp_UG_INSTALL}" <<-eot276if ! \${PW} groupshow $group >/dev/null 2>&1; then77echo "Creating group '$group' with gid '$gid'"78\${PW} groupadd $group -g $gid || exit \$?79else80echo "Using existing group '$group'"81fi82eot283done <<-eot84$(grep -h "^${group}:" ${dp_GID_FILES} | head -n 1)85eot86done87fi8889if [ -n "${USERS}" ]; then90for file in ${dp_UID_FILES}; do91if [ ! -f "${file}" ]; then92error "** ${file} doesn't exist. Exiting."93fi94done9596${dp_ECHO_MSG} "===> Creating users"97echo "echo \"===> Creating users\"" >> "${dp_UG_INSTALL}"9899for user in ${USERS}; do100# _bgpd:*:130:130:BGP Daemon:/var/empty:/sbin/nologin101if ! grep -q "^${user}:" ${dp_UID_FILES} ; then102error "** Cannot find any information about user \`${user}' in ${dp_UID_FILES}"103fi104while read -r line; do105# Do not change IFS for more than one command, if we106# changed IFS around the while read, it would mess up107# the string splitting in the heredoc command.108o_IFS=${IFS}109IFS=":"110set -- ${line}111IFS=${o_IFS}112login=$1113uid=$3114gid=$4115class=$5116gecos=$8117homedir=$9118shell=${10}119if [ -z "$uid" ] || [ -z "$gid" ] || [ -z "$homedir" ] || [ -z "$shell" ]; then120error "User line for ${user} is invalid"121fi122uid=$((uid+dp_UID_OFFSET))123gid=$((gid+dp_GID_OFFSET))124if [ -n "$class" ]; then125class="-L $class"126fi127homedir=$(echo "$homedir" | sed "s|^/usr/local|${dp_PREFIX}|")128cat >> "${dp_UG_INSTALL}" <<-eot2129if ! \${PW} usershow $login >/dev/null 2>&1; then130echo "Creating user '$login' with uid '$uid'"131\${PW} useradd $login -u $uid -g $gid $class -c "$gecos" -d $homedir -s $shell || exit \$?132else133echo "Using existing user '$login'"134fi135eot2136case $homedir in137/|/nonexistent|/var/empty)138;;139*)140echo "echo \"===> Creating homedir(s)\"" >> "${dp_UG_INSTALL}"141group=$(awk -F: -v gid=${gid} '$1 !~ /^#/ && $3 == gid { print $1 }' ${dp_GID_FILES})142cat >> "${dp_UG_INSTALL}" <<-blah143if [ -n "\${PKG_ROOTDIR}" ] && [ "\${PKG_ROOTDIR}" != "/" ]; then144HOMEDIR="\${PKG_ROOTDIR}/$homedir"145MDBDIR="-N \${PKG_ROOTDIR}/etc/"146else147HOMEDIR="$homedir"148MDBDIR=""149fi150${dp_INSTALL} \${MDBDIR} -d -g $group -o $login \${HOMEDIR}151blah152;;153esac154done <<-eot155$(grep -h "^${user}:" ${dp_UID_FILES} | head -n 1)156eot157done158fi159160if [ -n "${GROUPS}" ]; then161for group in ${GROUPS}; do162# mail:*:6:postfix,clamav163while read -r line; do164# Do not change IFS for more than one command, if we165# changed IFS around the while read, it would mess up166# the string splitting in the heredoc command.167o_IFS=${IFS}168IFS=":"169# As some lines do not have a fourth argument, provide170# one so $4 always exists.171set -- ${line} ""172IFS=${o_IFS}173group=$1174gid=$3175members=$4176gid=$((gid+dp_GID_OFFSET))177o_IFS=${IFS}178IFS=","179set -- ${members}180IFS=${o_IFS}181for login in "$@"; do182for user in ${USERS}; do183if [ -n "${user}" ] && [ "${user}" = "${login}" ]; then184cat >> "${dp_UG_INSTALL}" <<-eot2185if ! \${PW} groupshow ${group} | grep -qw ${login}; then186echo "Adding user '${login}' to group '${group}'"187\${PW} groupmod ${group} -m ${login} || exit \$?188fi189eot2190fi191done192done193done <<-eot194$(grep -h "^${group}:" ${dp_GID_FILES} | head -n 1)195eot196done197fi198199if [ -n "${USERS}" ]; then200for user in ${USERS}; do201if ! echo "${dp_USERS_BLACKLIST}" | grep -qw "${user}"; then202cat >> "${dp_UG_DEINSTALL}" <<-eot203if \${PW} usershow ${user} >/dev/null 2>&1; then204echo "==> You should manually remove the \"${user}\" user"205fi206eot207fi208done209fi210211if [ -n "${GROUPS}" ]; then212for group in ${GROUPS}; do213if ! echo "${dp_GROUPS_BLACKLIST}" | grep -qw "${group}"; then214cat >> "${dp_UG_DEINSTALL}" <<-eot215if \${PW} groupshow ${group} >/dev/null 2>&1; then216echo "==> You should manually remove the \"${group}\" group"217fi218eot219fi220done221fi222223224