branch Attitude Adjustment packages
[12.09/packages.git] / net / mac-to-devinfo / files / mac-to-devinfo
1 #!/bin/sh
2 #    mac-to-devinfo - MAC IEEE and custom information
3 #    Copyright (C) 2009  Daniel Dickinson
4
5 #    This program is free software; you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License as published by
7 #    the Free Software Foundation; either version 2 of the License, or
8 #    (at your option) any later version.
9
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14
15 #    You should have received a copy of the GNU General Public License along
16 #    with this program; if not, write to the Free Software Foundation, Inc.,
17 #    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 UCIBIN=uci
20 UCIFUNC=/usr/lib/mac-to-devinfo/devinfo-functions.sh
21 OVERRIDETABLE=mactodevinfo
22 DEVDB=macdevdb
23 UCICONFIG=/etc/config
24
25 OUIURL=http://standards.ieee.org/regauth/oui/oui.txt
26 IABURL=http://standards.ieee.org/regauth/oui/iab.txt
27
28 OUIDIR=/var/cache/mac-to-devinfo
29
30 OUIFILE=$OUIDIR/oui.txt
31 IABFILE=$OUIDIR/iab.txt
32
33 UCIDEVDB=/usr/share/mac-to-devinfo
34 UCISTATE="$(mktemp -t)"
35
36 . $UCIFUNC
37
38
39 if [ "$1" == "-x" ]; then
40     PORT="$2"
41     if [ -z "$PORT" ]; then
42         echo "-x without the port"
43         echo "Usage: $0 [-x port] MAC-address"
44         exit 1
45     fi
46     shift
47     shift
48 fi
49
50 MAC="$1"
51
52 if [ -z "$MAC" ]; then
53         echo "Unknown MAC address | Unknown | unknown | unknown | unknown"
54         exit 1
55 fi
56
57 MAC=$(echo $MAC | sed -e 's/^\([0-9]\):/0\1:/g' | sed -e 's/:\([0-9]\)$/:0\1/g' | sed -e 's/:\([0-9]\):/:0\1:/g')
58
59 parse_override() {
60         local cfg="$1"
61         config_get name "$cfg" name
62         config_get maclow "$cfg" maclow
63         config_get machigh "$cfg" machigh
64         config_get vendor "$cfg" vendor
65         config_get devtype "$cfg" devtype
66         config_get model "$cfg" model
67         config_get ouiowneroverride "$cfg" ouiowner
68         LOW=$(echo $maclow | sed -e 's/://g')
69         HIGH=$(echo $machigh | sed -e 's/://g')
70         if [ -z "$LOW" ] || [ -z "$HIGH" ] || [ -n "$(echo $LOW | grep -E -v '^[0-9A-Fa-f]{12}$')" ] || [ -n "$(echo $HIGH | grep -E -v '^[0-9A-Fa-f]{12}$')" ]; then
71                 echo "Invalid mac range for $name: $ouiowneroverride $vendor $devtype $model : $LOW-$HIGH" >&2
72                 return 1
73         fi
74
75         LOWMSB=$(expr substr "$LOW" 1 6)
76         LOWLSB=$(expr substr "$LOW" 7 6)
77         HIGHMSB=$(expr substr "$HIGH" 1 6)
78         HIGHLSB=$(expr substr "$HIGH" 7 6)
79
80         if [ -z "$LOWMSB" ] || [ -z "$LOWLSB" ] || [ -z "$HIGHMSB" ] || [ -z "$HIGHLSB" ]; then
81                 echo "Error splitting database address strings"
82                 return 1
83         fi
84
85         MACMSB=$(expr substr "$MACNUM" 1 6)
86         MACLSB=$(expr substr "$MACNUM" 7 6)
87
88         if [ -z "$MACMSB" ] || [ -z "$MACLSB" ]; then
89                 echo "Error spliiting mac address string"
90                 return 1
91         fi
92
93         if [ -n "$ouiowneroverride" ]; then
94             OUIOWNER="$ouiowneroverride"
95         fi
96         if [ $((0x$MACMSB)) -ge $((0x$LOWMSB)) ] && [ $((0x$MACMSB)) -le $((0x$HIGHMSB)) ]; then
97                 if [ $((0x$MACLSB)) -ge $((0x$LOWLSB)) ] && [ $((0x$MACLSB)) -le $((0x$HIGHLSB)) ]; then
98                         if [ "$PORT" == "5060" ]; then
99                             if [ "$vendor" == "Vertical Communications" ]; then
100                                 devtype="VoIP Phone"
101                                 model="IP200x"
102                             fi
103                         fi
104                         if [ "$PORT" == "6060" ]; then
105                             if [ "$vendor" == "Vertical Communications" ]; then
106                                 devtype="VoIP PBX"
107                                 model="Xcelerator IP"
108                             fi
109                         fi
110                         if [ -n "$OUTSTRING" ]; then    
111                                 echo "ERROR: We already have found information '$OUTSTRING'" >&2
112                                 echo -n "and now we have: " >&2
113                                 echo "$MAC | $OUIOWNER | $vendor | $devtype | $model" >&2
114                                 return 1
115                         else
116                                 echo "$MAC | $OUIOWNER | $vendor | $devtype | $model"
117                                 return 0
118                         fi
119                 fi
120         fi
121         return 0
122 }
123
124 if [ ! -r "$OUIFILE" ] || [ ! -r "$IABFILE" ]; then
125         echo "Missing IEEE Registration Authority Database.  Please run prep-devinfo"
126         exit 1
127 fi      
128
129 OUI="$(echo $MAC | cut -f1-3 -d:  | sed -e 's/:/-/g' | tr abcdef ABCDEF )"
130
131 OUIOWNER="$(cat $OUIFILE | grep $OUI | tr -s '\t' | cut -f2- -d"$(printf '\t')" )"
132
133 if [ -z "$OUIOWNER" ] || [ "$OUIOWNER" = "IEEE REGISTRATION AUTHORITY" ]; then
134         if [ -n "$(cat $IABFILE | grep $OUI)" ]; then
135                 LOWRANGE="$(echo $MAC | cut -f4-6 -d:  | sed -e 's/://g' | tr abcdef ABCDEF )"
136                 for range in $(cat $IABFILE |grep -E '[0-9A-F]{6}\-[0-9A-F]{6}' |cut -f1 -d"$(printf '\t')"  ); do
137                         LOW="$(echo $range | cut -f1 -d- )"
138                         HIGH="$(echo $range | cut -f2 -d- )"
139                         if [ -z "$LOW" ] || [ -z "$HIGH" ]; then
140                                 echo "Error parsing IAB table"
141                                 exit 1
142
143                         fi
144                         LOWMSB=$(expr substr "$LOW" 1 6)
145                         LOWLSB=$(expr substr "$LOW" 7 6)
146                         HIGHMSB=$(expr substr "$HIGH" 1 6)
147                         HIGHLSB=$(expr substr "$HIGH" 7 6)
148
149                         if [ -z "$LOWMSB" ] || [ -z "$LOWLSB" ] || [ -z "$HIGHMSB" ] || [ -z "$HIGHLSB" ]; then
150                                 echo "Error splitting registration authority IAB address strings"
151                                 exit 1
152                         fi
153
154                         MACMSB=$(expr substr "$LOWRANGE" 1 6)
155                         MACLSB=$(expr substr "$LOWRANGE" 7 6)
156                         
157                         if [ -z "$MACMSB" ] || [ -z "$MACLSB" ]; then
158                                 echo "Error splitting mac address string"
159                                 exit 1
160                         fi
161                         if [ $((0x$MACMSB)) -ge $((0x$LOWMSB)) ] && [ $((0x$MACMSB)) -le $((0x$HIGHMSB)) ]; then
162                                 if [ $((0x$MACLSB)) -ge $((0x$LOWLSB)) ] && [ $((0x$MACLSB)) -le $((0x$HIGHLSB)) ]; then
163                                         OUIOWNER="$(cat $IABFILE | grep "$LOW-$HIGH" | tr -s '\t' | cut -f3- -d"$(printf '\t')" )"
164                                         break;
165                                 fi
166                         fi
167                 done
168         fi
169 fi
170
171 if [ -z "$OUIOWNER" ]; then
172     OUIOWNER="Unknown Organization"
173 fi
174
175 MACNUM="$(echo $MAC |sed -e 's/://g' | tr ABCDEF abcdef )"
176
177 UCI="$UCIBIN -c $UCIDEVDB"
178 config_load $DEVDB
179 DEVSTRING=$(config_foreach parse_override mactodevinfo)
180
181 SAVEDEVSTRING="$DEVSTRING"
182
183 UCI="$UCIBIN -c $UCICONFIG"
184 config_load $OVERRIDETABLE
185 DEVSTRING=$(config_foreach parse_override mactodevinfo)
186
187 if [ -z "$DEVSTRING" ]; then
188     if [ -z "$SAVEDEVSTRING" ]; then
189         echo "$MAC | $OUIOWNER | unknown | unknown | unknown"
190     else
191         echo "$SAVEDEVSTRING"
192     fi
193 else
194     echo "$DEVSTRING"
195 fi
196
197 rm -f $UCISTATE