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