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