]> git.saurik.com Git - apple/network_cmds.git/blob - unbound/contrib/unbound_cache.sh
c3dd9c3a2df939ec3ca8f111fe95681212f85084
[apple/network_cmds.git] / unbound / contrib / unbound_cache.sh
1 #!/sbin/sh
2 #
3 # --------------------------------------------------------------
4 # -- DNS cache save/load script
5 # --
6 # -- Version 1.0
7 # -- By Yuri Voinov (c) 2006, 2014
8 # --------------------------------------------------------------
9 #
10 # ident "@(#)unbound_cache.sh 1.1 14/04/26 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 GETOPT=`which getopt`
31 ID=`which id`
32 PRINTF=`which printf`
33
34 ###############
35 # Subroutines #
36 ###############
37
38 usage_note ()
39 {
40 # Script usage note
41 $ECHO "Usage: `$BASENAME $0` [-s] or [-l] or [-r] or [-h]"
42 $ECHO
43 $ECHO "l - Load - default mode. Warming up Unbound DNS cache from saved file. cache-ttl must be high value."
44 $ECHO "s - Save - save Unbound DNS cache contents to plain file with domain names."
45 $ECHO "r - Reload - reloadind new cache entries and refresh existing cache"
46 $ECHO "h - this screen."
47 $ECHO "Note: Run without any arguments will be in default mode."
48 $ECHO " Also, unbound-control must be configured."
49 exit 0
50 }
51
52 root_check ()
53 {
54 if [ ! `$ID | $CUT -f1 -d" "` = "uid=0(root)" ]; then
55 $ECHO "ERROR: You must be super-user to run this script."
56 exit 1
57 fi
58 }
59
60 check_uc ()
61 {
62 if [ ! -f "$UC" ]; then
63 $ECHO .
64 $ECHO "ERROR: $UC not found. Exiting..."
65 exit 1
66 fi
67 }
68
69 check_saved_file ()
70 {
71 if [ ! -f "$CONF/$FNAME" ]; then
72 $ECHO .
73 $ECHO "ERROR: File $CONF/$FNAME does not exists. Save it first."
74 exit 1
75 fi
76 }
77
78 save_cache ()
79 {
80 # Save unbound cache
81 $PRINTF "Saving cache in $CONF/$FNAME..."
82 $UC dump_cache>$CONF/$FNAME
83 $ECHO "ok"
84 }
85
86 load_cache ()
87 {
88 # Load saved cache contents and warmup DNS cache
89 $PRINTF "Loading cache from saved $CONF/$FNAME..."
90 check_saved_file
91 $CAT $CONF/$FNAME|$UC load_cache
92 }
93
94 reload_cache ()
95 {
96 # Reloading and refresh existing cache and saved dump
97 save_cache
98 load_cache
99 }
100
101 ##############
102 # Main block #
103 ##############
104
105 # Root check
106 root_check
107
108 # Check unbound-control
109 check_uc
110
111 # Check command-line arguments
112 if [ "x$1" = "x" ]; then
113 # If arguments list empty, load cache by default
114 load_cache
115 else
116 arg_list=$1
117 # Parse command line
118 set -- `$GETOPT sSlLrRhH: $arg_list` || {
119 usage_note 1>&2
120 }
121
122 # Read arguments
123 for i in $arg_list
124 do
125 case $i in
126 -s | -S) save_cache;;
127 -l | -L) load_cache;;
128 -r | -R) reload_cache;;
129 -h | -H | \?) usage_note;;
130 esac
131 break
132 done
133 fi
134
135 exit 0