#!/bin/sh #---------------------------------------------------------------------------- # /var/install/bin/add-user-to-additional-group - add user to additional group # # Creation: 2003-07-20 fm # Last Update: $Id$ # # Copyright (c) 2001-@@YEAR@@ the eisfair team, team(at)eisfair(dot)org # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------- # include eislib . /var/install/include/eislib #debug=true if ${debug:-false} then exec 2>/tmp/$(basename ${0})-trace$$.log set -x ask_debug=true export ask_debug fi ASK=/var/install/bin/ask MKTEMP=/usr/bin/mktemp RM=/usr/bin/rm GREP=/usr/bin/grep AWK=/usr/bin/gawk SED=/usr/bin/sed USERMOD=/usr/sbin/usermod #---------------------------------------------------------------------------- # usage #---------------------------------------------------------------------------- usage() { cat <&2 fi } #---------------------------------------------------------------------------- # add user to group #---------------------------------------------------------------------------- add_user_to_group() { user=${1} ag=${2} #------------------------------------------------------------------------ # get user name #------------------------------------------------------------------------ if "${interactive}" then clrhome mecho --info "Add user to additional group" mecho mecho _ask_tmpfile=$(${MKTEMP} -t XXXXXXXXXXXXX) ${ASK} "User:" '' '*' > ${_ask_tmpfile} rc=${?} read user < ${_ask_tmpfile} ${RM} -f ${_ask_tmpfile} if [ ${rc} = 255 ] then exit 1 fi fi if [ -z "${user}" ] then mecho myecho --warn "Command aborted" do_exit 1 fi mecho line=$(${GREP} "^${user}:" /etc/passwd) if [ -z "${line}" ] then mecho myecho --warn "User ${user} does not exist!" do_exit 1 fi # get GID of primary group primgid=$(${AWK} -F: '/'^${user}':/ { print $4 }' /etc/passwd) groups='' free_groups='' oldifs="${IFS}" IFS=':' while read line do set -- ${line} g="${1}" gid="${3}" u="${4}" IFS=',' set -- ${u} group_found=false for j in ${*} do if [ ${j} = ${user} ] then group_found=true break fi done if ${group_found:-false} then if [ -z "${groups}" ] then groups="${g}" else groups="${groups} "${g} fi else # check for primgid if [ $gid != $primgid ] then if [ -z "${free_groups}" ] then free_groups="${g}" else free_groups="${free_groups} ${g}" fi fi fi IFS=':' done ${_ask_tmpfile} rc=${?} read ag <${_ask_tmpfile} ${RM} -f ${_ask_tmpfile} if [ $rc = 255 ] then exit 1 fi fi myecho if [ -z "${ag}" ] then mecho myecho --warn "Command aborted" do_exit 1 fi group_found=false for j in ${free_groups} do if [ ${ag} = ${j} ] then group_found=true break fi done if ! ${group_found:-false} then mecho myecho --warn "additional group ${ag} for user ${user} not found!" do_exit 1 fi if [ -n "${groups}" ] then g=$(echo "${groups} ${ag}" | ${SED} 's/ /,/g') else g=${ag} fi ${USERMOD} -G "${g}" ${user} } #---------------------------------------------------------------------------- # main #---------------------------------------------------------------------------- main() { user='' group='' interactive=false if [ ${#} -eq 0 ] then interactive=true add_user_to_group exit 0 fi if [ ${#} -lt 4 ] then usage exit 1 fi while [ ${#} -ne 0 ] do case ${1} in -u|--user) user="${2}" shift; shift ;; -g|--group) group="${2}" shift; shift ;; *) usage exit 1 ;; esac done add_user_to_group ${user} ${group} exit 0 } #---------------------------------------------------------------------------- # call function main #---------------------------------------------------------------------------- main "${@}" #---------------------------------------------------------------------------- # end #----------------------------------------------------------------------------