fix typos
[freeradius.git] / scripts / raddebug
1 #!/bin/sh
2 ######################################################################
3 #
4 #    This program is free software; you can redistribute it and/or modify
5 #    it under the terms of the GNU General Public License as published by
6 #    the Free Software Foundation; either version 2 of the License, or
7 #    (at your option) any later version.
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program; if not, write to the Free Software
16 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 #
18 #    Copyright (C) 2009 Network RADIUS SARL <info@networkradius.com>
19 #
20 ######################################################################
21 #
22 #  This script assumes that "radmin" is in PATH, and that the user
23 #  running this script has permission to connect to the radmin socket,
24 #  and to read/write files in the "logdir" directory.  If none of this is
25 #  true, then it won't work.
26 #
27 #   Usage: raddebug [-c condition] [-i client-ip-address] [-I client-ipv6-address] [-f socket_file] [-t timeout]  [-u username]
28 #
29 #
30
31 usage() {
32     printf "Usage: %s: [-c condition] [-d directory] [-D dictdir]  [-i client-ip-address] [-I client-ipv6-address] [-f socket_file] [-t timeout] [-u user]\n" $(basename $0) >&2
33     exit 2
34 }
35
36 extra=
37 condition=1
38 timeout=60
39 while getopts 'd:D:c:i:I:f:t:u:' OPTION
40 do
41   case $OPTION in
42   c)    condition="$OPTARG"
43         ;;
44   d)    extra="$extra -d $OPTARG"
45         ;;
46   D)    extra="$extra -D $OPTARG"
47         ;;
48   i)    x="(Packet-Src-IP-Address == $OPTARG)"
49         if [ "$condition" = "" ]; then
50             condition="$x"
51         else
52           condition="$condition && $x"
53         fi
54         ;;
55   I)    x="(Packet-Src-IPv6-Address == $OPTARG)"
56         if [ "$condition" = "" ]; then
57             condition="$x"
58         else
59           condition="$condition && $x"
60         fi
61         ;;
62   f)    extra="$extra -f $OPTARG"
63         ;;
64   t)    timeout="$OPTARG"
65         [ "$timeout" = "0" ] && timeout=1000000
66         ;;
67   u)    x="(User-Name == '$OPTARG')"
68         if [ "$condition" = "" ]; then
69             condition="$x"
70         else
71           condition="$condition && $x"
72         fi
73         ;;
74   ?)    usage
75         ;;
76   esac
77 done
78 shift $(($OPTIND - 1))
79
80 radmin="radmin $extra"
81
82 #
83 #  Start off by turning off debugging.
84 #  If there's a problem, die.
85 #
86 $radmin -e "debug condition"
87 if [ "$?" != "0" ]; then
88   exit 1
89 fi
90
91 #
92 #  Debug to a file, and then tell us where the file is.
93 #
94 outfile=`$radmin -e "debug file radmin.debug.$$" -e "show debug file"`
95 group=`$radmin -e "debug file radmin.debug.$$" -e "show config security.group"`
96
97 #
98 #  If there was an error setting the debug output, re-set the
99 #  debug condition, echo the error, and exit.
100 #
101 echo $outfile | grep 'ERROR' >/dev/null 2>&1
102 if [ "$?" = "0" ]; then
103   $radmin -e "debug condition"
104   echo $outfile
105   exit 1
106 fi
107
108 #
109 #  Truncate the file, and ensure it's writable by radiusd
110 #
111 cp /dev/null $outfile
112 [ "$group" != "" ] && chgrp $group $outfile
113 chmod g+w $outfile
114
115 TAILPID=$$
116
117 #
118 #  Set the trap to clean up on exit and any interrupts.
119 #
120 trap '$radmin -e "debug condition" -e "debug file"; rm -f $outfile;kill -TERM $TAILPID;exit 0' 1 2 15
121
122 #
123 #  Set the debug condition
124 #
125 $radmin -e "debug condition \"$condition\"" | grep -I 'error'
126 if [ $? -eq 0 ]; then
127     exit 1
128 fi
129
130 #
131 #  Print the output, and wait for "timeout".  Then, stop printing.
132 #
133 tail -f $outfile &
134 TAILPID=$!
135 sleep $timeout
136 kill -TERM $TAILPID
137 $radmin -e "debug condition" -e "debug file"
138 rm -f $outfile