aboutsummaryrefslogtreecommitdiff
path: root/external/unbound/contrib/unbound_cache.sh
blob: c3dd9c3a2df939ec3ca8f111fe95681212f85084 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/sbin/sh
#
# --------------------------------------------------------------
# -- DNS cache save/load script
# --
# -- Version 1.0
# -- By Yuri Voinov (c) 2006, 2014
# --------------------------------------------------------------
#
# ident   "@(#)unbound_cache.sh     1.1     14/04/26 YV"
#

#############
# Variables #
#############

# Installation base dir
CONF="/etc/opt/csw/unbound"
BASE="/opt/csw"

# Unbound binaries
UC="$BASE/sbin/unbound-control"
FNAME="unbound_cache.dmp"

# OS utilities
BASENAME=`which basename`
CAT=`which cat`
CUT=`which cut`
ECHO=`which echo`
GETOPT=`which getopt`
ID=`which id`
PRINTF=`which printf`

###############
# Subroutines #
###############

usage_note ()
{
# Script usage note
 $ECHO "Usage: `$BASENAME $0` [-s] or [-l] or [-r] or [-h]"
 $ECHO
 $ECHO "l - Load - default mode. Warming up Unbound DNS cache from saved file. cache-ttl must be high value."
 $ECHO "s - Save - save Unbound DNS cache contents to plain file with domain names."
 $ECHO "r - Reload - reloadind new cache entries and refresh existing cache"
 $ECHO "h - this screen."
 $ECHO "Note: Run without any arguments will be in default mode."
 $ECHO "      Also, unbound-control must be configured."
 exit 0
}

root_check ()
{
 if [ ! `$ID | $CUT -f1 -d" "` = "uid=0(root)" ]; then
  $ECHO "ERROR: You must be super-user to run this script."
  exit 1
 fi
}

check_uc ()
{
 if [ ! -f "$UC" ]; then
  $ECHO .
  $ECHO "ERROR: $UC not found. Exiting..."
  exit 1
 fi
}

check_saved_file ()
{
 if [ ! -f "$CONF/$FNAME" ]; then
  $ECHO .
  $ECHO "ERROR: File $CONF/$FNAME does not exists. Save it first."
  exit 1
 fi
}

save_cache ()
{
 # Save unbound cache
 $PRINTF "Saving cache in $CONF/$FNAME..."
 $UC dump_cache>$CONF/$FNAME
 $ECHO "ok"
}

load_cache ()
{
 # Load saved cache contents and warmup DNS cache
 $PRINTF "Loading cache from saved $CONF/$FNAME..."
 check_saved_file
 $CAT $CONF/$FNAME|$UC load_cache
}

reload_cache ()
{
 # Reloading and refresh existing cache and saved dump
 save_cache
 load_cache
}

##############
# Main block #
##############

# Root check
root_check

# Check unbound-control
check_uc

# Check command-line arguments
if [ "x$1" = "x" ]; then
# If arguments list empty, load cache by default
 load_cache
else
 arg_list=$1
 # Parse command line
 set -- `$GETOPT sSlLrRhH: $arg_list` || {
  usage_note 1>&2
 }

  # Read arguments
 for i in $arg_list
  do
   case $i in
    -s | -S) save_cache;;
    -l | -L) load_cache;;
    -r | -R) reload_cache;;
    -h | -H | \?) usage_note;;
   esac
   break
  done
fi

exit 0