A DynamicLookupCommand exiting with 10 is treated like an empty server stanza.
[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     /bin/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         /bin/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
36         fi
37     done
38 }
39
40 host_it_srv() {
41     ${HOSTCMD} -t srv $SRV_HOST | sort -k5 | 
42     while read line; do
43         set $line ; PORT=$7 ; HOST=$8 
44         /bin/echo -e "\thost ${HOST%.}:${PORT}"
45     done
46 }
47
48 host_it_naptr() {
49     ${HOSTCMD} -t naptr ${REALM} | grep x-eduroam:radius.tls | sort -k5 | 
50     while read line; do
51         set $line ; TYPE=$7 ; HOST=${10}
52         if [ "$TYPE" = "\"s\"" ]; then
53             SRV_HOST=${HOST%.}
54             host_it_srv
55         fi
56     done
57 }
58
59 if [ -x "${DIGCMD}" ]; then
60     SERVERS=$(dig_it_naptr)
61 elif [ -x "${HOSTCMD}" ]; then
62     SERVERS=$(host_it_naptr)
63 else
64     /bin/echo "${0} requires either \"dig\" or \"host\" command."
65     exit 1
66 fi
67
68 if [ -n "${SERVERS}" ]; then
69     /bin/echo -e "server dynamic_radsec.${REALM} {\n${SERVERS}\n\ttype TLS\n}"
70     exit 0
71 fi
72
73 exit 10                         # No server found.