|
| 1 | +#!/bin/sh |
| 2 | +# |
| 3 | +# ULA Generator |
| 4 | +# SUZUKI, Shinsuke <[email protected]> |
| 5 | +# based on Holger Zuleger's script |
| 6 | +# @(#) generate-uniq-local-ipv6-unicast-addr.sh |
| 7 | +# (c) Sep 2004 Holger Zuleger |
| 8 | +# |
| 9 | +# Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, and 2002 WIDE Project. |
| 10 | +# All rights reserved. |
| 11 | +# |
| 12 | +# Redistribution and use in source and binary forms, with or without |
| 13 | +# modification, are permitted provided that the following conditions |
| 14 | +# are met: |
| 15 | +# 1. Redistributions of source code must retain the above copyright |
| 16 | +# notice, this list of conditions and the following disclaimer. |
| 17 | +# 2. Redistributions in binary form must reproduce the above copyright |
| 18 | +# notice, this list of conditions and the following disclaimer in the |
| 19 | +# documentation and/or other materials provided with the distribution. |
| 20 | +# 3. Neither the name of the project nor the names of its contributors |
| 21 | +# may be used to endorse or promote products derived from this software |
| 22 | +# without specific prior written permission. |
| 23 | +# |
| 24 | +# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
| 25 | +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
| 28 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 30 | +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 | +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 | +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 33 | +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | +# SUCH DAMAGE. |
| 35 | +# |
| 36 | +# $Id: gen-ula.cgi,v 1.11 2013/05/20 22:43:57 suz Exp $ |
| 37 | +# |
| 38 | + |
| 39 | + |
| 40 | +# |
| 41 | +#site-specific parameters |
| 42 | +# |
| 43 | + |
| 44 | +#NTP server name |
| 45 | +ntpserv="clk.notemachi.wide.ad.jp" |
| 46 | + |
| 47 | +#ntpdate program |
| 48 | +ntpdate=/usr/sbin/ntpdate |
| 49 | + |
| 50 | +#the caller of this CGI script |
| 51 | +url="http://www.kame.net/~suz/gen-ula.html" |
| 52 | + |
| 53 | +#list of typical bogus lower 24-bits in MAC address |
| 54 | +typical_mac_list="010203 000000 000001 000102 123456" |
| 55 | + |
| 56 | +#SHA hash calculator |
| 57 | +sha=/usr/local/bin/sha |
| 58 | + |
| 59 | +#IEEE OUI Database (available at http::/standards.ieee.org/regauth/oui/oui.txt) |
| 60 | +ieeeoui=oui.txt |
| 61 | + |
| 62 | +# |
| 63 | +# main routine |
| 64 | +# |
| 65 | +#print-out header |
| 66 | +echo Content-type: text/html |
| 67 | +echo |
| 68 | + |
| 69 | +cat << EOH |
| 70 | +<body> |
| 71 | +<head> |
| 72 | +<title> |
| 73 | +Generated ULA |
| 74 | +</title> |
| 75 | +</head> |
| 76 | +<body> |
| 77 | +EOH |
| 78 | + |
| 79 | +#main routine |
| 80 | + |
| 81 | +#1. NTP-date |
| 82 | +date=`$ntpdate -dq $ntpserv 2> /dev/null | |
| 83 | + sed -n 's/^transmit timestamp: *\([^ ]*\) .*/\1/p' | |
| 84 | + tr -d "."` |
| 85 | + |
| 86 | +#2. EUI64 |
| 87 | +# obtains a MAC address |
| 88 | +# sed 's/\(.*\)=\(..:..:..:..:..:..\).*$/\2/' | |
| 89 | +mac=`echo $QUERY_STRING | sed 's/%3A/:/g' | |
| 90 | + tr '[:lower:]' '[:upper:]' | |
| 91 | + sed 's/\(.*\)=\(..:..:..:..:..:..\).*$/\2/'`; |
| 92 | +machex=`echo $mac | tr -d ':'` |
| 93 | +#length check |
| 94 | +len1=`echo $machex | wc -m` |
| 95 | +#character check |
| 96 | +len2=`echo $machex | tr -d [:xdigit:] | wc -m` |
| 97 | +if [ $len1 != 13 -o $len2 != 1 ]; then |
| 98 | + echo "<h1>Error</h1>" |
| 99 | + echo "<p>MAC-address $mac is invalid" |
| 100 | + echo "<p>Please take care of the following points: |
| 101 | + <ul> |
| 102 | + <li>each octet must be separeted by colon (e.g. 00-01-02-03-04-05 is not permitted) |
| 103 | + <li>leading 0 of each octet cannot be omitted (e.g. 3:4:5:4:5:9 is not permitted) |
| 104 | + <li>each octet consists of 0-9 and A-F. |
| 105 | + </ul></p>" |
| 106 | + echo "<a href="$url">go back to the previous page</a></body>" |
| 107 | + exit |
| 108 | +fi |
| 109 | + |
| 110 | +# MAC Vendor check |
| 111 | +machexvendor=`echo $machex | cut -c1-6` |
| 112 | +macvendor=`grep "^ $machexvendor" $ieeeoui | cut -c23-` |
| 113 | +if [ -z "$macvendor" ]; then |
| 114 | + echo "<h1>Error</h1>" |
| 115 | + echo "<p>MAC-address $mac" |
| 116 | + echo "is not registered to IEEE</p>" |
| 117 | + echo "<p>Please use the REAL MAC address</p>" |
| 118 | + echo "<a href="$url">go back to the previous page</a></body>" |
| 119 | + exit |
| 120 | +fi |
| 121 | + |
| 122 | +# booby trap:-) |
| 123 | +machexid=`echo $machex | cut -c7-12` |
| 124 | +for i in $typical_mac_list; do |
| 125 | + if [ $machexid = $i ]; then |
| 126 | + echo "<h1>Error</h1>" |
| 127 | + echo "<p>MAC-address $mac" |
| 128 | + echo "is a typical non-existing MAC address that is frequently used when a user is reluctant to find his/her own MAC address:-)</p>" |
| 129 | + echo "<p>Please use the REAL MAC address</p>" |
| 130 | + echo "<a href="$url">go back to the previous page</a></body>" |
| 131 | + exit |
| 132 | + fi |
| 133 | +done |
| 134 | + |
| 135 | +# generates EUI64 from the MAC address |
| 136 | +first=`echo $machex | cut -c1-1` |
| 137 | +second=`echo $machex | cut -c2-2` |
| 138 | +macu=`echo $machex | cut -c3-6` |
| 139 | +macl=`echo $machex | cut -c7-12` |
| 140 | + |
| 141 | +# reversing u/l bit |
| 142 | +case $second in |
| 143 | +[13579BDF]) |
| 144 | + echo "<h1>Error</h1>" |
| 145 | + echo "<p>MAC-address = $mac is a group MAC address</p>" |
| 146 | + echo "<a href="$url">go back to the previous page</a></body>" |
| 147 | + exit |
| 148 | + ;; |
| 149 | +0) |
| 150 | + second_rev=2 |
| 151 | + ;; |
| 152 | +2) |
| 153 | + second_rev=0 |
| 154 | + ;; |
| 155 | +4) |
| 156 | + second_rev=6; |
| 157 | + ;; |
| 158 | +6) |
| 159 | + second_rev=4; |
| 160 | + ;; |
| 161 | +8) |
| 162 | + second_rev=a; |
| 163 | + ;; |
| 164 | +A) |
| 165 | + second_rev=8; |
| 166 | + ;; |
| 167 | +C) |
| 168 | + second_rev=e; |
| 169 | + ;; |
| 170 | +E) |
| 171 | + second_rev=c; |
| 172 | + ;; |
| 173 | +*) |
| 174 | + #impossible |
| 175 | + echo "<h1>Error</h1>" |
| 176 | + echo "<p>MAC-address = $mac" |
| 177 | + echo "is registered to the IEEE database, but the first octet is regarded as invalid. (probably a bug in this script...) ($first$second)</p>" |
| 178 | + echo "<a href="$url">go back to the previous page</a></body>" |
| 179 | + exit |
| 180 | +esac |
| 181 | +eui64="${first}${second_rev}${macu}fffe${macl}" |
| 182 | + |
| 183 | +globalid=`echo $date$eui64 | ${sha} -1 | cut -c23-32` |
| 184 | +echo "<center>" |
| 185 | +echo "<h1>" |
| 186 | +echo "Generated ULA=" |
| 187 | +echo fd${globalid} | sed "s|\(....\)\(....\)\(....\)|\1:\2:\3::/48|" |
| 188 | +echo "</center>" |
| 189 | +echo "</h1>" |
| 190 | + |
| 191 | +cat << TAIL |
| 192 | +<ul> |
| 193 | +<li>MAC address=$mac ($macvendor) |
| 194 | +<li>EUI64 address=$eui64 |
| 195 | +<li>NTP date=$date |
| 196 | +</ul> |
| 197 | +
|
| 198 | +<a href="$url">go back to the previous page</a> |
| 199 | +</body> |
| 200 | +TAIL |
| 201 | +exit |
0 commit comments