summaryrefslogtreecommitdiff
path: root/tools/naptr-eduroam.sh
blob: 6497549735df09d70e4521e1649a12ac7e7f8852 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#! /bin/sh

# Example script!
# This script looks up radsec srv records in DNS for the one
# realm given as argument, and creates a server template based
# on that. It currently ignores weight markers, but does sort
# servers on priority marker, lowest number first.
# For host command this is column 5, for dig it is column 1.

usage() {
    echo "Usage: ${0} <realm>"
    exit 1
}

test -n "${1}" || usage

REALM="${1}"
DIGCMD=$(command -v dig)
HOSTCMD=$(command -v host)

dig_it_srv() {
    ${DIGCMD} +short srv $SRV_HOST | sort -n -k1 |
    while read line; do
	set $line ; PORT=$3 ; HOST=$4
	echo -e "\thost ${HOST%.}:${PORT}"
    done
}

dig_it_naptr() {
    ${DIGCMD} +short naptr ${REALM} | grep x-eduroam:radius.tls | sort -n -k1 |
    while read line; do
	set $line ; TYPE=$3 ; HOST=$6
	if [ "$TYPE" = "\"s\"" ]; then
	    SRV_HOST=${HOST%.}
	    dig_it_srv
	fi
    done
}

host_it_srv() {
    ${HOSTCMD} -t srv $SRV_HOST | sort -n -k5 |
    while read line; do
	set $line ; PORT=$7 ; HOST=$8 
	echo -e "\thost ${HOST%.}:${PORT}"
    done
}

host_it_naptr() {
    ${HOSTCMD} -t naptr ${REALM} | grep x-eduroam:radius.tls | sort -n -k5 |
    while read line; do
	set $line ; TYPE=$7 ; HOST=${10}
	if [ "$TYPE" = "\"s\"" ]; then
	    SRV_HOST=${HOST%.}
	    host_it_srv
	fi
    done
}

if [ -x "${DIGCMD}" ]; then
    SERVERS=$(dig_it_naptr)
elif [ -x "${HOSTCMD}" ]; then
    SERVERS=$(host_it_naptr)
else
    echo "${0} requires either \"dig\" or \"host\" command."
    exit 1
fi

if [ -n "${SERVERS}" ]; then
    echo -e "server dynamic_radsec.${REALM} {\n${SERVERS}\n\ttype TLS\n}"
    exit 0
fi

exit 10				# No server found.