Merge branch 'libradsec-new-client' into libradsec
[radsecproxy.git] / tools / radsec-dynsrv.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 digaaa)
19 HOSTCMD=$(command -v host)
20
21 dig_it() {
22    ${DIGCMD} +short srv _radsec._tcp.${REALM} | 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 host_it() {
30    ${HOSTCMD} -t srv _radsec._tcp.${REALM} | sort -k5 | 
31    while read line ; do
32       set $line ; PORT=$7 ; HOST=$8 
33       echo -e "\thost ${HOST%.}:${PORT}"
34    done
35 }
36
37 if test -x "${DIGCMD}" ; then
38    SERVERS=$(dig_it)
39 elif test -x "${HOSTCMD}" ; then
40    SERVERS=$(host_it)
41 else
42    echo "${0} requires either \"dig\" or \"host\" command."
43    exit 1
44 fi
45
46 if test -n "${SERVERS}" ; then
47         echo -e "server dynamic_radsec.${REALM} {\n${SERVERS}\n\ttype TLS\n}"
48         exit 0
49 fi
50
51 exit 0