1#!/bin/sh
2
3# Shell Script for updating the Geo-location databases by downloading
4# the latest delegation statistics files of the 5 RIR's.
5#
6# Created by Dr. Rolf Jansen on 2016-07-15.
7# Copyright (c) 2016. All rights reserved.
8#
9# Redistribution and use in source and binary forms, with or without modification,
10# are permitted provided that the following conditions are met:
11#
12# 1. Redistributions of source code must retain the above copyright notice,
13#    this list of conditions and the following disclaimer.
14#
15# 2. Redistributions in binary form must reproduce the above copyright notice,
16#    this list of conditions and the following disclaimer in the documentation
17#    and/or other materials provided with the distribution.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
20# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
22# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
29# verify the path to the fetch utility
30if [ -e "/usr/bin/fetch" ]; then
31   FETCH="/usr/bin/fetch"
32elif [ -e "/usr/local/bin/fetch" ]; then
33   FETCH="/usr/local/bin/fetch"
34elif [ -e "/opt/local/bin/fetch" ]; then
35   FETCH="/opt/local/bin/fetch"
36else
37   echo "No fetch utility can be found on the system -- Stopping."
38   echo "On Mac OS X, execute either 'sudo port install fetch' or install"
39   echo "fetch from source into '/usr/local/bin', and then try again."
40   exit 1
41fi
42
43if [ "$1" == "" ]; then
44#  ?IRROR="ftp.afrinic.net" -- as of 2016-07-15, AFRINIC does not mirror the data of the other RIR's
45#  MIRROR="ftp.apnic.net"   -- the RIPE directory is named ripe-ncc instead of ripencc
46#  ?IRROR="ftp.arin.net"    -- as of 2016-07-15, the data of the other RIR's seem to be heavily outdated
47#  MIRROR="ftp.lacnic.net"
48   MIRROR="ftp.ripe.net"
49else
50   MIRROR="$1"
51fi
52
53if [ $MIRROR == "" ]; then
54   exit 1
55fi
56
57if [ $MIRROR == "ftp.afrinic.net" ] || [ $MIRROR == "ftp.arin.net" ]; then
58   echo "ARIN and AFRINIC do not reliably mirror the data of the other RIR's -- Stopping."
59   exit 1
60fi
61
62if [ $MIRROR == "ftp.apnic.net" ]; then
63   RIPEDIR="ripe-ncc"
64else
65   RIPEDIR="ripencc"
66fi
67
68# Storage location of configuration files and the IP-Ranges database files
69IPRanges="/usr/local/etc/ipdb/IPRanges"
70if [ ! -d "$IPRanges" ]; then
71   mkdir -p "$IPRanges"
72fi
73
74# AFRINIC IPv4 ranges
75rm -f "$IPRanges/afrinic.md5"
76$FETCH -o "$IPRanges/afrinic.md5" "ftp://$MIRROR/pub/stats/afrinic/delegated-afrinic-extended-latest.md5" && \
77$FETCH -o "$IPRanges/afrinic.dat" "ftp://$MIRROR/pub/stats/afrinic/delegated-afrinic-extended-latest"
78if [ -f "$IPRanges/afrinic.md5" ] && [ -f "$IPRanges/afrinic.dat" ]; then
79   stored_md5=`/usr/bin/cut -d " " -f4 "$IPRanges/afrinic.md5"`
80   actual_md5=`/sbin/md5 -q "$IPRanges/afrinic.dat"`
81   if [ "$stored_md5" != "$actual_md5" ]; then
82      exit 1
83   fi
84else
85   exit 1
86fi
87
88# APNIC IPv4 ranges
89rm -f "$IPRanges/apnic.md5"
90$FETCH -o "$IPRanges/apnic.md5" "ftp://$MIRROR/pub/stats/apnic/delegated-apnic-extended-latest.md5" && \
91$FETCH -o "$IPRanges/apnic.dat" "ftp://$MIRROR/pub/stats/apnic/delegated-apnic-extended-latest"
92if [ -f "$IPRanges/apnic.md5" ] && [ -f "$IPRanges/apnic.dat" ]; then
93   stored_md5=`/usr/bin/cut -d " " -f4 "$IPRanges/apnic.md5"`
94   actual_md5=`/sbin/md5 -q "$IPRanges/apnic.dat"`
95   if [ "$stored_md5" != "$actual_md5" ]; then
96      exit 1
97   fi
98else
99   exit 1
100fi
101
102# ARIN IPv4 ranges
103rm -f "$IPRanges/arin.md5"
104$FETCH -o "$IPRanges/arin.md5" "ftp://$MIRROR/pub/stats/arin/delegated-arin-extended-latest.md5" && \
105$FETCH -o "$IPRanges/arin.dat" "ftp://$MIRROR/pub/stats/arin/delegated-arin-extended-latest"
106if [ -f "$IPRanges/arin.md5" ] && [ -f "$IPRanges/arin.dat" ]; then
107   stored_md5=`/usr/bin/cut -d " " -f1 "$IPRanges/arin.md5"`
108   actual_md5=`/sbin/md5 -q "$IPRanges/arin.dat"`
109   if [ "$stored_md5" != "$actual_md5" ]; then
110      exit 1
111   fi
112else
113   exit 1
114fi
115
116# LACNIC IPv4 ranges
117rm -f "$IPRanges/lacnic.md5"
118$FETCH -o "$IPRanges/lacnic.md5" "ftp://$MIRROR/pub/stats/lacnic/delegated-lacnic-extended-latest.md5" && \
119$FETCH -o "$IPRanges/lacnic.dat" "ftp://$MIRROR/pub/stats/lacnic/delegated-lacnic-extended-latest"
120if [ -f "$IPRanges/lacnic.md5" ] && [ -f "$IPRanges/lacnic.dat" ]; then
121   stored_md5=`/usr/bin/cut -d " " -f4 "$IPRanges/lacnic.md5"`
122   actual_md5=`/sbin/md5 -q "$IPRanges/lacnic.dat"`
123   if [ "$stored_md5" != "$actual_md5" ]; then
124      exit 1
125   fi
126else
127   exit 1
128fi
129
130# RIPENCC IPv4 ranges
131rm -f "$IPRanges/ripencc.md5"
132$FETCH -o "$IPRanges/ripencc.md5" "ftp://$MIRROR/pub/stats/$RIPEDIR/delegated-ripencc-extended-latest.md5" && \
133$FETCH -o "$IPRanges/ripencc.dat" "ftp://$MIRROR/pub/stats/$RIPEDIR/delegated-ripencc-extended-latest"
134if [ -f "$IPRanges/ripencc.md5" ] && [ -f "$IPRanges/ripencc.dat" ]; then
135   stored_md5=`/usr/bin/cut -d " " -f4 "$IPRanges/ripencc.md5"`
136   actual_md5=`/sbin/md5 -q "$IPRanges/ripencc.dat"`
137   if [ "$stored_md5" != "$actual_md5" ]; then
138      exit 1
139   fi
140else
141   exit 1
142fi
143
144/usr/local/bin/ipdb "$IPRanges/ipcc.bst" \
145                    "$IPRanges/afrinic.dat" \
146                    "$IPRanges/apnic.dat" \
147                    "$IPRanges/arin.dat" \
148                    "$IPRanges/lacnic.dat" \
149                    "$IPRanges/ripencc.dat"
150