| 1 | #!/bin/sh |
| 2 | # Copyright © 2013 Filippo Giunchedi <[email protected]> |
| 3 | # Copyright © 2013-2016 Luciano Bello <[email protected]> |
| 4 | # Copyright © 2022 Samuel Henrique <[email protected]> |
| 5 | # This work is free. You can redistribute it and/or modify it under the |
| 6 | # terms of the Do What The Fuck You Want To Public License, Version 2, |
| 7 | # as published by Sam Hocevar. |
| 8 | # See the LICENSE file for more details. |
| 9 | |
| 10 | BASEDIR=${BASEDIR:-/var/lib/ieee-data/} |
| 11 | RUN_PARSERS=${RUN_PARSERS:-1} |
| 12 | OLD="5" |
| 13 | FORCE=false |
| 14 | QUIET=false |
| 15 | |
| 16 | set -e |
| 17 | |
| 18 | files_to_get="https://standards-oui.ieee.org/oui/oui.txt|oui.txt |
| 19 | https://standards-oui.ieee.org/oui28/mam.txt|mam.txt |
| 20 | https://standards-oui.ieee.org/oui36/oui36.txt|oui36.txt |
| 21 | https://standards-oui.ieee.org/iab/iab.txt|iab.txt |
| 22 | https://standards-oui.ieee.org/oui/oui.csv|oui.csv |
| 23 | https://standards-oui.ieee.org/oui28/mam.csv|mam.csv |
| 24 | https://standards-oui.ieee.org/oui36/oui36.csv|oui36.csv |
| 25 | https://standards-oui.ieee.org/iab/iab.csv|iab.csv" |
| 26 | tmpf=$(mktemp -t "ieee-data_XXXXXX") |
| 27 | chmod 0644 "$tmpf" |
| 28 | |
| 29 | Die () { |
| 30 | $QUIET || printf "%s\n" "$1" |
| 31 | exit 1 |
| 32 | } |
| 33 | |
| 34 | checkTXT () { |
| 35 | TXTOUI='OUI.*\s*Organization\s*.*\s*Organization\s*Address' |
| 36 | head -n 6 "$1" | tr -d '\n' | grep -qe "$TXTOUI" |
| 37 | return $? |
| 38 | } |
| 39 | |
| 40 | |
| 41 | checkCSV () { |
| 42 | CSVOUI='Registry,Assignment,Organization Name,Organization Address' |
| 43 | head -n 1 "$1" | grep -qe "$CSVOUI" |
| 44 | return $? |
| 45 | } |
| 46 | |
| 47 | checkFileHead () { |
| 48 | $QUIET || printf "\tChecking header\n" |
| 49 | TXTOUI='OUI.*\s*Organization\s*.*\s*Organization\s*Address' |
| 50 | checkTXT "$1" || checkCSV "$1" || ( rm "$1" && Die "The downloaded file looks corrupted." ) |
| 51 | } |
| 52 | |
| 53 | checkPerm() { |
| 54 | $QUIET || printf "\tChecking permissions on %s\n" "$1" |
| 55 | touch -a "$1" || { Die "Touch on $1 exit with $?";} |
| 56 | } |
| 57 | |
| 58 | goAndGet () { |
| 59 | $QUIET || printf "Updating %s/%s\n" "$BASEDIR" "$2" |
| 60 | checkPerm "$BASEDIR/$2" |
| 61 | $QUIET || printf "\tDownloading %s to %s/%s\n" "$1" "$BASEDIR" "$2" |
| 62 | $dler "$1" > "$tmpf" || { Die "$dler $1 exit with $?";} |
| 63 | checkFileHead "$tmpf" |
| 64 | $QUIET || printf "\tTemporary location %s to be moved to %s/%s\n" "$tmpf" "$BASEDIR" "$2" |
| 65 | mv -f "$tmpf" "$BASEDIR/$2" |
| 66 | $QUIET || printf "\t%s/%s updated.\n" "$BASEDIR" "$2" |
| 67 | } |
| 68 | |
| 69 | while getopts ":fq" opt; do |
| 70 | case $opt in |
| 71 | f) |
| 72 | FORCE=true |
| 73 | ;; |
| 74 | q) |
| 75 | QUIET=true |
| 76 | ;; |
| 77 | \?) |
| 78 | printf "Invalid option: -%s\n" "$OPTARG" >&2 |
| 79 | ;; |
| 80 | esac |
| 81 | done |
| 82 | |
| 83 | dler="" |
| 84 | [ -x "$(command -v curl)" ] && dler="curl -sL" |
| 85 | [ -x "$(command -v lwp-request)" ] && dler="lwp-request -m GET" |
| 86 | [ -x "$(command -v wget)" ] && dler="wget -q -O-" |
| 87 | |
| 88 | if [ -z "$dler" ]; then |
| 89 | Die "Unable to find a suitable downloader, please install wget or curl or libwww-perl" |
| 90 | fi |
| 91 | |
| 92 | cd "$BASEDIR" || { Die "can't cd to $BASEDIR"; } |
| 93 | |
| 94 | LASTUPDATE=$(cat "$BASEDIR/.lastupdate") |
| 95 | OLD_SECONDS=$(( OLD * 86400 )) |
| 96 | CURRENT=$(date +%s) |
| 97 | AGE=$(( CURRENT - LASTUPDATE )) |
| 98 | |
| 99 | if [ $FORCE = false ] && [ $AGE -le $OLD_SECONDS ]; then |
| 100 | Die "The files are kinda new yet (less than $OLD days old)" |
| 101 | fi |
| 102 | |
| 103 | for url_and_filename in $(printf "%s\n" "$files_to_get") |
| 104 | do |
| 105 | url=$(echo "$url_and_filename" | cut -d "|" -f1) |
| 106 | file_name=$(echo "$url_and_filename" | cut -d "|" -f2) |
| 107 | goAndGet "$url" "$file_name" |
| 108 | done |
| 109 | |
| 110 | rm "$BASEDIR/.lastupdate" |
| 111 | printf "%s\n" "$CURRENT" > "$BASEDIR/.lastupdate" |
| 112 | |
| 113 | if [ -x "$(command -v run-parts)" ] && [ -d update.d ] && [ "$RUN_PARSERS" -ne 0 ]; then |
| 114 | $QUIET || printf "\tRunning parsers from %s/update.d\n" "$BASEDIR" |
| 115 | run-parts -a "$BASEDIR" -a oui.txt update.d/ |
| 116 | run-parts -a "$BASEDIR" -a iab.txt update.d/ |
| 117 | run-parts -a "$BASEDIR" -a oui.cvs update.d/ |
| 118 | run-parts -a "$BASEDIR" -a iab.cvs update.d/ |
| 119 | fi |