radsecproxy-1.6.5.
[libradsec.git] / tools / naptr-eduroam.sh
1 #! /bin/sh
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 column 5, for dig it is column 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 PRINTCMD=$(command -v printf)
21
22 dig_it_srv() {
23     ${DIGCMD} +short srv $SRV_HOST | sort -n -k1 |
24     while read line; do
25         set $line ; PORT=$3 ; HOST=$4
26         $PRINTCMD "\thost ${HOST%.}:${PORT}\n"
27     done
28 }
29
30 dig_it_naptr() {
31     ${DIGCMD} +short naptr ${REALM} | grep x-eduroam:radius.tls | sort -n -k1 |
32     while read line; do
33         set $line ; TYPE=$3 ; HOST=$6
34         if [ "$TYPE" = "\"s\"" -o "$TYPE" = "\"S\"" ]; then
35             SRV_HOST=${HOST%.}
36             dig_it_srv
37         fi
38     done
39 }
40
41 host_it_srv() {
42     ${HOSTCMD} -t srv $SRV_HOST | sort -n -k5 |
43     while read line; do
44         set $line ; PORT=$7 ; HOST=$8 
45         $PRINTCMD "\thost ${HOST%.}:${PORT}\n"
46     done
47 }
48
49 host_it_naptr() {
50     ${HOSTCMD} -t naptr ${REALM} | grep x-eduroam:radius.tls | sort -n -k5 |
51     while read line; do
52         set $line ; TYPE=$7 ; HOST=${10}
53         if [ "$TYPE" = "\"s\"" -o "$TYPE" = "\"S\"" ]; then
54             SRV_HOST=${HOST%.}
55             host_it_srv
56         fi
57     done
58 }
59
60 if [ -x "${DIGCMD}" ]; then
61     SERVERS=$(dig_it_naptr)
62 elif [ -x "${HOSTCMD}" ]; then
63     SERVERS=$(host_it_naptr)
64 else
65     echo "${0} requires either \"dig\" or \"host\" command."
66     exit 1
67 fi
68
69 if [ -n "${SERVERS}" ]; then
70     $PRINTCMD "server dynamic_radsec.${REALM} {\n${SERVERS}\n\ttype TLS\n}\n"
71     exit 0
72 fi
73
74 exit 10                         # No server found.