Mon, 28 Jan 2013 17:37:18 +0100
Correct socket error reporting improvement with IPv6 portable code,
after helpful recommendation by Saúl Ibarra Corretgé on OSips devlist.
michael@428 | 1 | #!/bin/sh |
michael@428 | 2 | ## |
michael@428 | 3 | ## etc.usrgrp.sh -- user/group name/id determination |
michael@428 | 4 | ## Copyright (c) 2000-2012 OpenPKG GmbH <http://openpkg.com/> |
michael@428 | 5 | ## |
michael@428 | 6 | ## This software is property of the OpenPKG GmbH, DE MUC HRB 160208. |
michael@428 | 7 | ## All rights reserved. Licenses which grant limited permission to use, |
michael@428 | 8 | ## copy, modify and distribute this software are available from the |
michael@428 | 9 | ## OpenPKG GmbH. |
michael@428 | 10 | ## |
michael@428 | 11 | ## THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED |
michael@428 | 12 | ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
michael@428 | 13 | ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
michael@428 | 14 | ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR |
michael@428 | 15 | ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@428 | 16 | ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@428 | 17 | ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
michael@428 | 18 | ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
michael@428 | 19 | ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
michael@428 | 20 | ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
michael@428 | 21 | ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
michael@428 | 22 | ## SUCH DAMAGE. |
michael@428 | 23 | ## |
michael@428 | 24 | |
michael@428 | 25 | # command line parameters (defaults) |
michael@428 | 26 | help=0 |
michael@428 | 27 | usr=''; grp='' |
michael@428 | 28 | susr=''; sgrp='' |
michael@428 | 29 | musr=''; mgrp='' |
michael@428 | 30 | rusr=''; rgrp='' |
michael@428 | 31 | nusr=''; ngrp='' |
michael@428 | 32 | suid=''; sgid='' |
michael@428 | 33 | muid=''; mgid='' |
michael@428 | 34 | ruid=''; rgid='' |
michael@428 | 35 | nuid=''; ngid='' |
michael@428 | 36 | |
michael@428 | 37 | # parse command line options |
michael@428 | 38 | for opt |
michael@428 | 39 | do |
michael@428 | 40 | case $opt in |
michael@428 | 41 | -*=*) arg=`echo "$opt" | sed 's/^[-_a-zA-Z0-9]*=//'` ;; |
michael@428 | 42 | *) arg='' ;; |
michael@428 | 43 | esac |
michael@428 | 44 | case $opt in |
michael@428 | 45 | -h | --help ) help=1 ;; |
michael@428 | 46 | --usr=* | --user=* ) usr=$arg ;; |
michael@428 | 47 | --grp=* | --group=* ) grp=$arg ;; |
michael@428 | 48 | --susr=* ) susr=$arg ;; |
michael@428 | 49 | --sgrp=* ) sgrp=$arg ;; |
michael@428 | 50 | --musr=* ) musr=$arg ;; |
michael@428 | 51 | --mgrp=* ) mgrp=$arg ;; |
michael@428 | 52 | --rusr=* ) rusr=$arg ;; |
michael@428 | 53 | --rgrp=* ) rgrp=$arg ;; |
michael@428 | 54 | --nusr=* ) nusr=$arg ;; |
michael@428 | 55 | --ngrp=* ) ngrp=$arg ;; |
michael@428 | 56 | --suid=* ) suid=$arg ;; |
michael@428 | 57 | --sgid=* ) sgid=$arg ;; |
michael@428 | 58 | --muid=* ) muid=$arg ;; |
michael@428 | 59 | --mgid=* ) mgid=$arg ;; |
michael@428 | 60 | --ruid=* ) ruid=$arg ;; |
michael@428 | 61 | --rgid=* ) rgid=$arg ;; |
michael@428 | 62 | --nuid=* ) nuid=$arg ;; |
michael@428 | 63 | --ngid=* ) ngid=$arg ;; |
michael@428 | 64 | * ) help=1 ;; |
michael@428 | 65 | esac |
michael@428 | 66 | done |
michael@428 | 67 | if [ ".$help" = .1 ]; then |
michael@428 | 68 | echo "Usage: sh $0 [-h|--help]" 2>&1 |
michael@428 | 69 | echo " [--[smrn]?usr=<usr>] [--[smrn]?grp=<usr>]" 2>&1 |
michael@428 | 70 | echo " [--[smrn]uid=<uid>] [--[smrn]gid=<gid>]" 2>&1 |
michael@428 | 71 | exit 1 |
michael@428 | 72 | fi |
michael@428 | 73 | |
michael@428 | 74 | # determine cusr/cgrp |
michael@428 | 75 | cusr=`(id -un) 2>/dev/null ||\ |
michael@428 | 76 | (id | sed -e 's;^[^(]*(\([^)]*\)).*;\1;') 2>/dev/null ||\ |
michael@428 | 77 | (whoami) 2>/dev/null ||\ |
michael@428 | 78 | (who am i | cut "-d " -f1) 2>/dev/null ||\ |
michael@428 | 79 | echo $LOGNAME` |
michael@428 | 80 | cgid=`(id -g $cusr) 2>/dev/null ||\ |
michael@428 | 81 | ((getent passwd "${cusr}"; grep "^${cusr}:" /etc/passwd; ypmatch "${cusr}" passwd; nismatch "${cusr}" passwd; nidump passwd . | grep "^${cusr}:") 2>/dev/null |\ |
michael@428 | 82 | sed -n -e '1p' | awk -F: '{ print $4; }')` |
michael@428 | 83 | cgrp=`(id -gn $cusr) 2>/dev/null ||\ |
michael@428 | 84 | ((getent group; cat /etc/group; ypcat group; niscat group; nidump group .) 2>/dev/null | grep "^[^:]*:[^:]*:${cgid}:" |\ |
michael@428 | 85 | sed -n -e '1p' | awk -F: '{ print $1; }')` |
michael@428 | 86 | [ ".$cgrp" = . ] && cgrp="$cusr" |
michael@428 | 87 | |
michael@428 | 88 | # determine OpenPKG susr/sgrp |
michael@428 | 89 | if [ ".$susr" = . ]; then |
michael@428 | 90 | if [ ".$usr" = . ]; then |
michael@428 | 91 | susr="$cusr" |
michael@428 | 92 | else |
michael@428 | 93 | susr="root" |
michael@428 | 94 | fi |
michael@428 | 95 | fi |
michael@428 | 96 | if [ ".$sgrp" = . ]; then |
michael@428 | 97 | sgrp=`(id -gn $susr) 2>/dev/null` |
michael@428 | 98 | if [ ".$sgrp" = . ]; then |
michael@428 | 99 | tgid=`(getent passwd "${susr}"; grep "^${susr}:" /etc/passwd; ypmatch "${susr}" passwd; nismatch "${susr}" passwd; nidump passwd . | grep "^${susr}:") 2>/dev/null |\ |
michael@428 | 100 | sed -n -e '1p' | awk -F: '{ print $4; }'` |
michael@428 | 101 | if [ ".$tgid" != . ]; then |
michael@428 | 102 | sgid="${tgid}" |
michael@428 | 103 | sgrp=`(getent group; cat /etc/group; ypcat group; niscat group; nidump group .) 2>/dev/null |\ |
michael@428 | 104 | grep "^[^:]*:[^:]*:${sgid}:" | sed -n -e '1p' | awk -F: '{ print $1; }'` |
michael@428 | 105 | fi |
michael@428 | 106 | if [ ".$sgrp" = . ]; then |
michael@428 | 107 | sgrp="wheel" |
michael@428 | 108 | fi |
michael@428 | 109 | fi |
michael@428 | 110 | fi |
michael@428 | 111 | |
michael@428 | 112 | # determine OpenPKG musr/mgrp |
michael@428 | 113 | if [ ".$musr" = . ]; then |
michael@428 | 114 | musr="$usr" |
michael@428 | 115 | fi |
michael@428 | 116 | if [ ".$musr" = . ]; then |
michael@428 | 117 | musr="$cusr" |
michael@428 | 118 | fi |
michael@428 | 119 | if [ ".$mgrp" = . ]; then |
michael@428 | 120 | mgrp=`(id -gn $musr) 2>/dev/null` |
michael@428 | 121 | if [ ".$mgrp" = . ]; then |
michael@428 | 122 | tgid=`(getent passwd "${musr}"; grep "^${musr}:" /etc/passwd; ypmatch "${musr}" passwd; nismatch "${musr}" passwd; nidump passwd . | grep "^${musr}:") 2>/dev/null |\ |
michael@428 | 123 | sed -n -e '1p' | awk -F: '{ print $4; }'` |
michael@428 | 124 | if [ ".$tgid" != . ]; then |
michael@428 | 125 | mgid="${tgid}" |
michael@428 | 126 | mgrp=`(getent group; cat /etc/group; ypcat group; niscat group; nidump group .) 2>/dev/null |\ |
michael@428 | 127 | grep "^[^:]*:[^:]*:${mgid}:" | sed -n -e '1p' | awk -F: '{ print $1; }'` |
michael@428 | 128 | fi |
michael@428 | 129 | if [ ".$mgrp" = . ]; then |
michael@428 | 130 | mgrp="$grp" |
michael@428 | 131 | fi |
michael@428 | 132 | if [ ".$mgrp" = . ]; then |
michael@428 | 133 | mgrp="$cgrp" |
michael@428 | 134 | fi |
michael@428 | 135 | fi |
michael@428 | 136 | fi |
michael@428 | 137 | |
michael@428 | 138 | # determine OpenPKG rusr/rgrp |
michael@428 | 139 | if [ ".$rusr" = . ]; then |
michael@428 | 140 | rusr="${usr}-r" |
michael@428 | 141 | fi |
michael@428 | 142 | if [ ".$rusr" = ".-r" ]; then |
michael@428 | 143 | rusr="$cusr" |
michael@428 | 144 | fi |
michael@428 | 145 | if [ ".$rgrp" = . ]; then |
michael@428 | 146 | rgrp=`(id -gn $rusr) 2>/dev/null` |
michael@428 | 147 | if [ ".$rgrp" = . ]; then |
michael@428 | 148 | tgid=`(getent passwd "${rusr}"; grep "^${rusr}:" /etc/passwd; ypmatch "${rusr}" passwd; nismatch "${rusr}" passwd; nidump passwd . | grep "^${rusr}:") 2>/dev/null |\ |
michael@428 | 149 | sed -n -e '1p' | awk -F: '{ print $4; }'` |
michael@428 | 150 | if [ ".$tgid" != . ]; then |
michael@428 | 151 | rgid="${tgid}" |
michael@428 | 152 | rgrp=`(getent group; cat /etc/group; ypcat group; nismatch group; nidump group .) 2>/dev/null |\ |
michael@428 | 153 | grep "^[^:]*:[^:]*:${rgid}:" | sed -n -e '1p' | awk -F: '{ print $1; }'` |
michael@428 | 154 | fi |
michael@428 | 155 | if [ ".$rgrp" = . ]; then |
michael@428 | 156 | rgrp="${grp}-r" |
michael@428 | 157 | fi |
michael@428 | 158 | if [ ".$rgrp" = ".-r" ]; then |
michael@428 | 159 | rgrp="$cgrp" |
michael@428 | 160 | fi |
michael@428 | 161 | fi |
michael@428 | 162 | fi |
michael@428 | 163 | |
michael@428 | 164 | # determine OpenPKG nusr/ngrp |
michael@428 | 165 | if [ ".$nusr" = . ]; then |
michael@428 | 166 | nusr="${usr}-n" |
michael@428 | 167 | fi |
michael@428 | 168 | if [ ".$nusr" = ".-n" ]; then |
michael@428 | 169 | nusr="$cusr" |
michael@428 | 170 | fi |
michael@428 | 171 | if [ ".$ngrp" = . ]; then |
michael@428 | 172 | ngrp=`(id -gn $nusr) 2>/dev/null` |
michael@428 | 173 | if [ ".$ngrp" = . ]; then |
michael@428 | 174 | tgid=`(getent passwd "${nusr}"; grep "^${nusr}:" /etc/passwd; ypmatch "${nusr}" passwd; nismatch "${nusr}" passwd; nidump passwd . | grep "^${nusr}:") 2>/dev/null |\ |
michael@428 | 175 | sed -n -e '1p' | awk -F: '{ print $4; }'` |
michael@428 | 176 | if [ ".$tgid" != . ]; then |
michael@428 | 177 | ngid="${tgid}" |
michael@428 | 178 | ngrp=`(getent group; cat /etc/group; ypcat group; niscat group; nidump group .) 2>/dev/null |\ |
michael@428 | 179 | grep "^[^:]*:[^:]*:${ngid}:" | sed -n -e '1p' | awk -F: '{ print $1; }'` |
michael@428 | 180 | fi |
michael@428 | 181 | if [ ".$ngrp" = . ]; then |
michael@428 | 182 | ngrp="${grp}-n" |
michael@428 | 183 | fi |
michael@428 | 184 | if [ ".$ngrp" = ".-n" ]; then |
michael@428 | 185 | ngrp="$cgrp" |
michael@428 | 186 | fi |
michael@428 | 187 | fi |
michael@428 | 188 | fi |
michael@428 | 189 | |
michael@428 | 190 | # determine OpenPKG suid/sgid |
michael@428 | 191 | # (currently not necessary) |
michael@428 | 192 | |
michael@428 | 193 | # determine OpenPKG muid/mgid |
michael@428 | 194 | # (currently not necessary) |
michael@428 | 195 | |
michael@428 | 196 | # determine OpenPKG ruid/rgid |
michael@428 | 197 | # (currently not necessary) |
michael@428 | 198 | |
michael@428 | 199 | # determine OpenPKG nuid/ngid |
michael@428 | 200 | # (currently not necessary) |
michael@428 | 201 | |
michael@428 | 202 | # print results |
michael@428 | 203 | output="" |
michael@428 | 204 | for var in \ |
michael@428 | 205 | susr sgrp \ |
michael@428 | 206 | musr mgrp \ |
michael@428 | 207 | rusr rgrp \ |
michael@428 | 208 | nusr ngrp \ |
michael@428 | 209 | suid sgid \ |
michael@428 | 210 | muid mgid \ |
michael@428 | 211 | ruid rgid \ |
michael@428 | 212 | nuid ngid; do |
michael@428 | 213 | eval "val=\"\$$var\"" |
michael@428 | 214 | if [ ".$output" = . ]; then |
michael@428 | 215 | output="$var=\"$val\"" |
michael@428 | 216 | else |
michael@428 | 217 | output="$output; $var=\"$val\"" |
michael@428 | 218 | fi |
michael@428 | 219 | done |
michael@428 | 220 | echo $output |
michael@428 | 221 |