]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/contrib/unbound_cache.sh
MFV 2.0-rc2
[FreeBSD/FreeBSD.git] / contrib / unbound / contrib / unbound_cache.sh
1 #!/sbin/sh
2
3 # --------------------------------------------------------------
4 # -- DNS cache save/load script
5 # --
6 # -- Version 1.2
7 # -- By Yuri Voinov (c) 2006, 2014
8 # --------------------------------------------------------------
9 #
10 # ident   "@(#)unbound_cache.sh     1.2     14/10/30 YV"
11 #
12
13 #############
14 # Variables #
15 #############
16
17 # Installation base dir
18 CONF="/etc/opt/csw/unbound"
19 BASE="/opt/csw"
20
21 # Unbound binaries
22 UC="$BASE/sbin/unbound-control"
23 FNAME="unbound_cache.dmp"
24
25 # OS utilities
26 BASENAME=`which basename`
27 CAT=`which cat`
28 CUT=`which cut`
29 ECHO=`which echo`
30 EXPR=`which expr`
31 GETOPT=`which getopt`
32 ID=`which id`
33 LS=`which ls`
34
35 ###############
36 # Subroutines #
37 ###############
38
39 usage_note ()
40 {
41 # Script usage note
42  $ECHO "Usage: `$BASENAME $0` [-s] or [-l] or [-r] or [-h] [filename]"
43  $ECHO .
44  $ECHO "l - Load - default mode. Warming up Unbound DNS cache from saved file. cache-ttl must be high value."
45  $ECHO "s - Save - save Unbound DNS cache contents to plain file with domain names."
46  $ECHO "r - Reload - reloadind new cache entries and refresh existing cache"
47  $ECHO "h - this screen."
48  $ECHO "filename - file to save/load dumped cache. If not specified, $CONF/$FNAME will be used instead."
49  $ECHO "Note: Run without any arguments will be in default mode."
50  $ECHO "      Also, unbound-control must be configured."
51  exit 0
52 }
53
54 root_check ()
55 {
56  if [ ! `$ID | $CUT -f1 -d" "` = "uid=0(root)" ]; then
57   $ECHO "ERROR: You must be super-user to run this script."
58   exit 1
59  fi
60 }
61
62 check_uc ()
63 {
64  if [ ! -f "$UC" ]; then
65   $ECHO .
66   $ECHO "ERROR: $UC not found. Exiting..."
67   exit 1
68  fi
69 }
70
71 check_saved_file ()
72 {
73  filename=$1
74  if [ ! -z "$filename" -a ! -f "$filename" ]; then
75   $ECHO .
76   $ECHO "ERROR: File $filename does not exists. Save it first."
77   exit 1
78  elif [ ! -f "$CONF/$FNAME" ]; then
79   $ECHO .
80   $ECHO "ERROR: File $CONF/$FNAME does not exists. Save it first."
81   exit 1
82  fi
83 }
84
85 save_cache ()
86 {
87  # Save unbound cache
88  filename=$1
89  if [ -z "$filename" ]; then
90   $ECHO "Saving cache in $CONF/$FNAME..."
91   $UC dump_cache>$CONF/$FNAME
92   $LS -lh $CONF/$FNAME
93  else
94   $ECHO "Saving cache in $filename..."
95   $UC dump_cache>$filename
96   $LS -lh $filename
97  fi
98  $ECHO "ok"
99 }
100
101 load_cache ()
102 {
103  # Load saved cache contents and warmup cache
104  filename=$1
105  if [ -z "$filename" ]; then
106   $ECHO "Loading cache from saved $CONF/$FNAME..."
107   $LS -lh $CONF/$FNAME
108   check_saved_file $filename
109   $CAT $CONF/$FNAME|$UC load_cache
110  else
111   $ECHO "Loading cache from saved $filename..."
112   $LS -lh $filename
113   check_saved_file $filename
114   $CAT $filename|$UC load_cache
115  fi
116 }
117
118 reload_cache ()
119 {
120  # Reloading and refresh existing cache and saved dump
121  filename=$1
122  save_cache $filename
123  load_cache $filename
124 }
125
126 ##############
127 # Main block #
128 ##############
129
130 # Root check
131 root_check
132
133 # Check unbound-control
134 check_uc
135
136 # Check command-line arguments
137 if [ "x$*" = "x" ]; then
138  # If arguments list empty,load cache by default
139  load_cache
140 else
141  arg_list=$*
142  # Parse command line
143  set -- `$GETOPT sSlLrRhH: $arg_list` || {
144   usage_note 1>&2
145  }
146
147  # Read arguments
148  for i in $arg_list
149   do
150    case $i in
151     -s | -S) save="1";;
152     -l | -L) save="0";;
153     -r | -R) save="2";;
154     -h | -H | \?) usage_note;;
155     *) shift
156        file=$1
157        break;;
158    esac
159    shift
160   done
161
162  # Remove trailing --
163  shift `$EXPR $OPTIND - 1`
164 fi
165
166 if [ "$save" = "1" ]; then
167  save_cache $file
168 elif [ "$save" = "0" ]; then
169  load_cache $file
170 elif [ "$save" = "2" ]; then
171  reload_cache $file
172 fi
173
174 exit 0