michael@13: #!/bin/sh
michael@13: ##
michael@13: ## usrgrp.sh -- user/group name/id determination
michael@13: ## Copyright (c) 2000-2007 OpenPKG Foundation e.V.
michael@13: ## Copyright (c) 2000-2007 Ralf S. Engelschall
michael@13: ##
michael@13: ## Permission to use, copy, modify, and distribute this software for
michael@13: ## any purpose with or without fee is hereby granted, provided that
michael@13: ## the above copyright notice and this permission notice appear in all
michael@13: ## copies.
michael@13: ##
michael@13: ## THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
michael@13: ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
michael@13: ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
michael@13: ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
michael@13: ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@13: ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@13: ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
michael@13: ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
michael@13: ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
michael@13: ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
michael@13: ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
michael@13: ## SUCH DAMAGE.
michael@13: ##
michael@13:
michael@13: # command line parameters (defaults)
michael@13: help=0
michael@13: usr=''; grp=''
michael@13: susr=''; sgrp=''
michael@13: musr=''; mgrp=''
michael@13: rusr=''; rgrp=''
michael@13: nusr=''; ngrp=''
michael@13: suid=''; sgid=''
michael@13: muid=''; mgid=''
michael@13: ruid=''; rgid=''
michael@13: nuid=''; ngid=''
michael@13:
michael@13: # parse command line options
michael@13: for opt
michael@13: do
michael@13: case $opt in
michael@13: -*=*) arg=`echo "$opt" | sed 's/^[-_a-zA-Z0-9]*=//'` ;;
michael@13: *) arg='' ;;
michael@13: esac
michael@13: case $opt in
michael@13: -h | --help ) help=1 ;;
michael@13: --usr=* | --user=* ) usr=$arg ;;
michael@13: --grp=* | --group=* ) grp=$arg ;;
michael@13: --susr=* ) susr=$arg ;;
michael@13: --sgrp=* ) sgrp=$arg ;;
michael@13: --musr=* ) musr=$arg ;;
michael@13: --mgrp=* ) mgrp=$arg ;;
michael@13: --rusr=* ) rusr=$arg ;;
michael@13: --rgrp=* ) rgrp=$arg ;;
michael@13: --nusr=* ) nusr=$arg ;;
michael@13: --ngrp=* ) ngrp=$arg ;;
michael@13: --suid=* ) suid=$arg ;;
michael@13: --sgid=* ) sgid=$arg ;;
michael@13: --muid=* ) muid=$arg ;;
michael@13: --mgid=* ) mgid=$arg ;;
michael@13: --ruid=* ) ruid=$arg ;;
michael@13: --rgid=* ) rgid=$arg ;;
michael@13: --nuid=* ) nuid=$arg ;;
michael@13: --ngid=* ) ngid=$arg ;;
michael@13: * ) help=1 ;;
michael@13: esac
michael@13: done
michael@13: if [ ".$help" = .1 ]; then
michael@13: echo "Usage: sh $0 [-h|--help]" 2>&1
michael@13: echo " [--[smrn]?usr=] [--[smrn]?grp=]" 2>&1
michael@13: echo " [--[smrn]uid=] [--[smrn]gid=]" 2>&1
michael@13: exit 1
michael@13: fi
michael@13:
michael@13: # determine cusr/cgrp
michael@13: cusr=`(id -un) 2>/dev/null ||\
michael@13: (id | sed -e 's;^[^(]*(\([^)]*\)).*;\1;') 2>/dev/null ||\
michael@13: (whoami) 2>/dev/null ||\
michael@13: (who am i | cut "-d " -f1) 2>/dev/null ||\
michael@13: echo $LOGNAME`
michael@13: cgid=`(id -g $cusr) 2>/dev/null ||\
michael@13: ((getent passwd "${cusr}"; grep "^${cusr}:" /etc/passwd; ypmatch "${cusr}" passwd; nismatch "${cusr}" passwd; nidump passwd . | grep "^${cusr}:") 2>/dev/null |\
michael@13: sed -n -e '1p' | awk -F: '{ print $4; }')`
michael@13: cgrp=`(id -gn $cusr) 2>/dev/null ||\
michael@13: ((getent group; cat /etc/group; ypcat group; niscat group; nidump group .) 2>/dev/null | grep "^[^:]*:[^:]*:${cgid}:" |\
michael@13: sed -n -e '1p' | awk -F: '{ print $1; }')`
michael@13: [ ".$cgrp" = . ] && cgrp="$cusr"
michael@13:
michael@13: # determine OpenPKG susr/sgrp
michael@13: if [ ".$susr" = . ]; then
michael@13: if [ ".$usr" = . ]; then
michael@13: susr="$cusr"
michael@13: else
michael@13: susr="root"
michael@13: fi
michael@13: fi
michael@13: if [ ".$sgrp" = . ]; then
michael@13: sgrp=`(id -gn $susr) 2>/dev/null`
michael@13: if [ ".$sgrp" = . ]; then
michael@13: tgid=`(getent passwd "${susr}"; grep "^${susr}:" /etc/passwd; ypmatch "${susr}" passwd; nismatch "${susr}" passwd; nidump passwd . | grep "^${susr}:") 2>/dev/null |\
michael@13: sed -n -e '1p' | awk -F: '{ print $4; }'`
michael@13: if [ ".$tgid" != . ]; then
michael@13: sgid="${tgid}"
michael@13: sgrp=`(getent group; cat /etc/group; ypcat group; niscat group; nidump group .) 2>/dev/null |\
michael@13: grep "^[^:]*:[^:]*:${sgid}:" | sed -n -e '1p' | awk -F: '{ print $1; }'`
michael@13: fi
michael@13: if [ ".$sgrp" = . ]; then
michael@13: sgrp="wheel"
michael@13: fi
michael@13: fi
michael@13: fi
michael@13:
michael@13: # determine OpenPKG musr/mgrp
michael@13: if [ ".$musr" = . ]; then
michael@13: musr="$usr"
michael@13: fi
michael@13: if [ ".$musr" = . ]; then
michael@13: musr="$cusr"
michael@13: fi
michael@13: if [ ".$mgrp" = . ]; then
michael@13: mgrp=`(id -gn $musr) 2>/dev/null`
michael@13: if [ ".$mgrp" = . ]; then
michael@13: tgid=`(getent passwd "${musr}"; grep "^${musr}:" /etc/passwd; ypmatch "${musr}" passwd; nismatch "${musr}" passwd; nidump passwd . | grep "^${musr}:") 2>/dev/null |\
michael@13: sed -n -e '1p' | awk -F: '{ print $4; }'`
michael@13: if [ ".$tgid" != . ]; then
michael@13: mgid="${tgid}"
michael@13: mgrp=`(getent group; cat /etc/group; ypcat group; niscat group; nidump group .) 2>/dev/null |\
michael@13: grep "^[^:]*:[^:]*:${mgid}:" | sed -n -e '1p' | awk -F: '{ print $1; }'`
michael@13: fi
michael@13: if [ ".$mgrp" = . ]; then
michael@13: mgrp="$grp"
michael@13: fi
michael@13: if [ ".$mgrp" = . ]; then
michael@13: mgrp="$cgrp"
michael@13: fi
michael@13: fi
michael@13: fi
michael@13:
michael@13: # determine OpenPKG rusr/rgrp
michael@13: if [ ".$rusr" = . ]; then
michael@13: rusr="${usr}-r"
michael@13: fi
michael@13: if [ ".$rusr" = ".-r" ]; then
michael@13: rusr="$cusr"
michael@13: fi
michael@13: if [ ".$rgrp" = . ]; then
michael@13: rgrp=`(id -gn $rusr) 2>/dev/null`
michael@13: if [ ".$rgrp" = . ]; then
michael@13: tgid=`(getent passwd "${rusr}"; grep "^${rusr}:" /etc/passwd; ypmatch "${rusr}" passwd; nismatch "${rusr}" passwd; nidump passwd . | grep "^${rusr}:") 2>/dev/null |\
michael@13: sed -n -e '1p' | awk -F: '{ print $4; }'`
michael@13: if [ ".$tgid" != . ]; then
michael@13: rgid="${tgid}"
michael@13: rgrp=`(getent group; cat /etc/group; ypcat group; nismatch group; nidump group .) 2>/dev/null |\
michael@13: grep "^[^:]*:[^:]*:${rgid}:" | sed -n -e '1p' | awk -F: '{ print $1; }'`
michael@13: fi
michael@13: if [ ".$rgrp" = . ]; then
michael@13: rgrp="${grp}-r"
michael@13: fi
michael@13: if [ ".$rgrp" = ".-r" ]; then
michael@13: rgrp="$cgrp"
michael@13: fi
michael@13: fi
michael@13: fi
michael@13:
michael@13: # determine OpenPKG nusr/ngrp
michael@13: if [ ".$nusr" = . ]; then
michael@13: nusr="${usr}-n"
michael@13: fi
michael@13: if [ ".$nusr" = ".-n" ]; then
michael@13: nusr="$cusr"
michael@13: fi
michael@13: if [ ".$ngrp" = . ]; then
michael@13: ngrp=`(id -gn $nusr) 2>/dev/null`
michael@13: if [ ".$ngrp" = . ]; then
michael@13: tgid=`(getent passwd "${nusr}"; grep "^${nusr}:" /etc/passwd; ypmatch "${nusr}" passwd; nismatch "${nusr}" passwd; nidump passwd . | grep "^${nusr}:") 2>/dev/null |\
michael@13: sed -n -e '1p' | awk -F: '{ print $4; }'`
michael@13: if [ ".$tgid" != . ]; then
michael@13: ngid="${tgid}"
michael@13: ngrp=`(getent group; cat /etc/group; ypcat group; niscat group; nidump group .) 2>/dev/null |\
michael@13: grep "^[^:]*:[^:]*:${ngid}:" | sed -n -e '1p' | awk -F: '{ print $1; }'`
michael@13: fi
michael@13: if [ ".$ngrp" = . ]; then
michael@13: ngrp="${grp}-n"
michael@13: fi
michael@13: if [ ".$ngrp" = ".-n" ]; then
michael@13: ngrp="$cgrp"
michael@13: fi
michael@13: fi
michael@13: fi
michael@13:
michael@13: # determine OpenPKG suid/sgid
michael@13: # (currently not necessary)
michael@13:
michael@13: # determine OpenPKG muid/mgid
michael@13: # (currently not necessary)
michael@13:
michael@13: # determine OpenPKG ruid/rgid
michael@13: # (currently not necessary)
michael@13:
michael@13: # determine OpenPKG nuid/ngid
michael@13: # (currently not necessary)
michael@13:
michael@13: # print results
michael@13: output=""
michael@13: for var in \
michael@13: susr sgrp \
michael@13: musr mgrp \
michael@13: rusr rgrp \
michael@13: nusr ngrp \
michael@13: suid sgid \
michael@13: muid mgid \
michael@13: ruid rgid \
michael@13: nuid ngid; do
michael@13: eval "val=\"\$$var\""
michael@13: if [ ".$output" = . ]; then
michael@13: output="$var=\"$val\""
michael@13: else
michael@13: output="$output; $var=\"$val\""
michael@13: fi
michael@13: done
michael@13: echo $output
michael@13: