Merge branch 'libradsec-new-client' into libradsec
[radsecproxy.git] / tools / naptr-eduroam.sh
1 #! /bin/bash
2
3 # Example script!
4 # This script looks up radsec srv records in DNS for the one
5 # realm given as argument, and creates a server template based
6 # on that. It currently ignores weight markers, but does sort
7 # servers on priority marker, lowest number first.
8 # For host command this is coloumn 5, for dig it is coloumn 1.
9
10 usage() {
11    echo "Usage: ${0} <realm>"
12    exit 1
13 }
14
15 test -n "${1}" || usage
16
17 REALM="${1}"
18 DIGCMD=$(command -v dig)
19 HOSTCMD=$(command -v host)
20
21 dig_it_srv() {
22    ${DIGCMD} +short srv $SRV_HOST | sort -k1 |
23    while read line ; do
24       set $line ; PORT=$3 ; HOST=$4
25       echo -e "\thost ${HOST%.}:${PORT}"
26    done
27 }
28
29 dig_it_naptr() {
30    ${DIGCMD} +short naptr ${REALM} | grep x-eduroam:radius.tls | sort -k1 |
31    while read line ; do
32       set $line ; TYPE=$3 ; HOST=$6
33       if [ "$TYPE" == "\"s\"" ]; then { 
34         SRV_HOST=${HOST%.}
35         dig_it_srv; }; fi
36    done
37 }
38
39 host_it_srv() {
40    ${HOSTCMD} -t srv $SRV_HOST | sort -k5 | 
41    while read line ; do
42       set $line ; PORT=$7 ; HOST=$8 
43       echo -e "\thost ${HOST%.}:${PORT}"
44    done
45 }
46
47 host_it_naptr() {
48    ${HOSTCMD} -t naptr ${REALM} | grep x-eduroam:radius.tls | sort -k5 | 
49    while read line ; do
50       set $line ; TYPE=$7 ; HOST=${10}
51       if [ "$TYPE" == "\"s\"" ]; then {
52         SRV_HOST=${HOST%.}
53         host_it_srv; }; fi
54
55    done
56 }
57
58 if test -x "${DIGCMD}" ; then
59    SERVERS=$(dig_it_naptr)
60 elif test -x "${HOSTCMD}" ; then
61    SERVERS=$(host_it_naptr)
62 else
63    echo "${0} requires either \"dig\" or \"host\" command."
64    exit 1
65 fi
66
67 if test -n "${SERVERS}" ; then
68         echo -e "server dynamic_radsec.${REALM} {\n${SERVERS}\n\ttype TLS\n}"
69         exit 0
70 fi
71
72 exit 0