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
10usage() {
11    echo "Usage: ${0} <realm>"
12    exit 1
13}
14
15test -n "${1}" || usage
16
17REALM="${1}"
18DIGCMD=$(command -v dig)
19HOSTCMD=$(command -v host)
20PRINTCMD=$(command -v printf)
21
22dig_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
30dig_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
41host_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
49host_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
60if [ -x "${DIGCMD}" ]; then
61    SERVERS=$(dig_it_naptr)
62elif [ -x "${HOSTCMD}" ]; then
63    SERVERS=$(host_it_naptr)
64else
65    echo "${0} requires either \"dig\" or \"host\" command."
66    exit 1
67fi
68
69if [ -n "${SERVERS}" ]; then
70    $PRINTCMD "server dynamic_radsec.${REALM} {\n${SERVERS}\n\ttype TLS\n}\n"
71    exit 0
72fi
73
74exit 10				# No server found.
75