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