update Sat Sep 12 12:37:01 PDT 2009
[pkgsrc.git] / wip / audit-system / files / audit-system
1 #! @SH@
2 #
3 # $NetBSD$
4 #
5 # Copyright (c) 2008 The NetBSD Foundation, Inc.
6 # All rights reserved.
7 #
8 # This code is derived from software contributed to The NetBSD Foundation
9 # by Alistair Crooks.
10 #
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions
13 # are met:
14 # 1. Redistributions of source code must retain the above copyright
15 #    notice, this list of conditions and the following disclaimer.
16 # 2. Redistributions in binary form must reproduce the above copyright
17 #    notice, this list of conditions and the following disclaimer in the
18 #    documentation and/or other materials provided with the distribution.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 #
32
33 AWK=@AWK@
34 CHMOD=@CHMOD@
35 CKSUM=@CKSUM@
36 MKDIR="@MKDIR@"
37 FIND=@FIND@
38 MV=@MV@
39 RM=@RM@
40
41 FORMAT_MAJOR=1
42 FORMAT_MINOR=0
43 FORMAT_TEENY=0
44
45 # print a usage message and then die
46 usage() {
47         argv0="${1##*/}"
48         cat <<EOF
49 $2
50 Usage: $argv0 [-duv]
51     -d : Download the system-vulnerabilities file before anything else
52     -u : Download the system-vulnerabilities file only
53     -v : Verbose mode
54 EOF
55         exit 1
56 }
57
58 # set some defaults
59 : ${SYSVULNDIR=@SYSVULNDIR@}
60 : ${FETCH_PROTO=ftp}
61 : ${FETCH_TOOL=@FETCH_TOOL@}
62 : ${FETCH_TOOL_ARGS="-o"}
63 scriptdir="@SCRIPT_DIR@"
64 download=no
65 download_only=no
66 verbose=no
67 neednew=no
68 tools="@STAT@ @OBJDUMP@ @CKSUM@ @FETCH_TOOL@ @IDENT@"
69
70 # check the integrity of a system-vulnerabilities file
71 check_integrity() 
72 {
73         # see if the file got damaged while it was being downloaded
74         recordedsum=$(${AWK} '$1 == "#CHECKSUM" { print $3 }' $1)
75         recordedalg=$(${AWK} '$1 == "#CHECKSUM" { print $2 }' $1)
76
77         if [ "x${recordedsum}" = "x" -o "x${recordedalg}" = "x" ]; then
78                 echo "ERROR: Error in downloading"
79                 ${RM} -f "$1"
80                 exit 1
81         fi
82         
83         calcsum=$(${AWK} '$1 == "#CHECKSUM" || /\$NetBSD.*/ { next } { print }' $1 | ${CKSUM} -a ${recordedalg})
84         if [ "x${recordedsum}" != "x${calcsum}" ]; then
85                 echo "ERROR: Checksum mismatch"
86                 ${RM} -f "$1"
87                 exit 1
88         fi
89 }
90
91 # check all the tools we need are available
92 for t in ${tools} ; do
93         if [ ! -x ${t} ]; then
94                 echo "ERROR: Required tools not found"
95                 exit 1
96         fi
97 done
98
99 # process any command line arguments
100 while [ $# -gt 0 ]; do
101         case "$1" in
102         -d)     download=yes ;;
103         -u)     download_only=yes ;;
104         -v)     verbose=yes ;;
105         *)      usage "$0" "Unknown option $1"
106         esac
107         shift
108 done
109
110 # check for incompatible command line options
111 if [ "x${download}" = "xyes" -a "x${download_only}" = "xyes" ]; then
112         echo "ERROR: Invalid command line options specified"
113         exit 1
114 fi
115
116 # if we have any configuration information, then read it
117 if [ -r @PKG_SYSCONFDIR@/audit-system.conf ]; then
118         if [ "x${verbose}" = "xyes" ]; then
119                 echo "Reading settings from @PKG_SYSCONFDIR@/audit-system.conf"
120         fi
121         . @PKG_SYSCONFDIR@/audit-system.conf
122 fi
123
124 # check FETCH_PROTO is sane
125 case ${FETCH_PROTO} in
126 http)   ;;
127 ftp)    ;;
128 *)      echo "ERROR: Unsupported FETCH_PROTO specified"
129         exit 1
130         ;;
131 esac
132
133 # setup what we know is sane so far
134 #vulsource="${FETCH_PROTO}://ftp.NetBSD.org/pub/NetBSD/misc/agc/audit-system/system-vulnerabilities"
135 vulsource="${FETCH_PROTO}://localhost/pub/NetBSD/system-vulnerabilities"
136 vuls="${SYSVULNDIR}/system-vulnerabilities"
137 newvullist="${SYSVULNDIR}/system-vulnerabilities.$$"
138
139 # try to download the system vulnerability list, as requested
140 # the integrity of the list is checked below
141 # so just issue a warning if there was a failure
142 if [ "x${download}" = "xyes" -o "x${download_only}" = "xyes" ]; then
143
144         # check we can get to ${SYSVULNDIR}
145         if [ ! -d ${SYSVULNDIR}/. ]; then
146                 echo "Creating ${SYSVULNDIR}"
147                 ${MKDIR} ${SYSVULNDIR}
148         fi
149         echo audit-system > ${SYSVULNDIR}/.cookie
150         if [ -f ${SYSVULNDIR}/.cookie ]; then
151                 ${RM} ${SYSVULNDIR}/.cookie
152         else
153                 echo "ERROR: Unable to write to ${SYSVULNDIR}"
154                 exit 1
155         fi
156
157         # get the file
158         ${FETCH_TOOL} ${FETCH_TOOL_ARGS} "${newvullist}" "${vulsource}"
159
160         # see if we got a file
161         if [ ! -f "${newvullist}" ]; then
162                 echo "ERROR: Download of vulnerabilities file failed"
163                 exit 1
164         fi
165
166         check_integrity "${newvullist}"
167
168         # test to see if file has been changed
169         if [ -f "${vuls}" ]; then
170                 oldsum=$(${AWK} '$1 == "#CHECKSUM" { print $3 }' "${vuls}")
171                 if [ "x${oldsum}" != "x${calcsum}" ]; then
172                         neednew=yes
173                 fi
174         else
175                 neednew=yes
176         fi
177
178         # if we need the new file, move it into position
179         if [ "x${neednew}" = "xyes" ]; then
180                 echo "System vulnerabilities file has been updated"
181                 ${CHMOD} a+r "${newvullist}"
182                 ${MV} -f "${newvullist}" "${vuls}"
183         else
184                 echo "No change from existing system vulnerabilities file"
185                 ${RM} -f "${newvullist}"
186         fi
187 fi
188
189 # if download_only was specified then we end here
190 if [ "x${download_only}" = "xyes" ]; then
191         exit 0
192 fi
193
194 # check for missing vulnerabilities file before we continue
195 if [ ! -f "${vuls}" ]; then
196         echo "ERROR: Missing system vulnerabilities file"
197         exit 1
198 fi
199
200 # check for old vulnerabilities file if we're being verbose
201 if [ "x${verbose}" = "xyes" ]; then
202         if [ -n "$(${FIND} ${vuls} -ctime +7)" ]; then
203                 echo "WARNING: system vulnerabilites file is more than a week old"
204         fi
205 fi
206
207 # check format version of vulnerabilities file
208 file_major=$(${AWK} '/^#[ \t]*FORMAT/ { split($NF, a, "\\."); print a[1]; exit; }' "${vuls}")
209 file_minor=$(${AWK} '/^#[ \t]*FORMAT/ { split($NF, a, "\\."); print a[2]; exit; }' "${vuls}")
210 file_teeny=$(${AWK} '/^#[ \t]*FORMAT/ { split($NF, a, "\\."); print a[3]; exit; }' "${vuls}")
211
212 if [ -z "${file_teeny}" ] ; then
213         file_teeny=0
214 fi
215
216 if [ "x${file_major}" = "x" -o "x${file_minor}" = "x" ]; then
217         echo "ERROR: Error in downloading"
218         exit 1
219 fi
220
221 if [ "${file_major}" -gt "${FORMAT_MAJOR}" -o "${file_minor}" -gt "${FORMAT_MINOR}" ]; then
222         echo "ERROR: Unsupported file format version"
223         echo "Please ensure you are running the latest version of audit-system"
224         exit 1
225 fi
226
227 if [ "${file_major}" -lt "${FORMAT_MAJOR}" -o "${file_minor}" -lt "${FORMAT_MINOR}" ]; then
228         echo "ERROR: Old version of system-vulnerabilities file detected"
229         exit 1
230 fi
231
232 # check integrity of vulnerabilities file
233 check_integrity "${vuls}"
234
235 ${AWK} -v scriptdir="${scriptdir}" '
236 /^#.*/  { next }
237 {
238         cmd = sprintf("v=$(%s/audit-%s %s) && test -e %s && test $v -lt %s && echo %c%s (version $v before %s) could have a %s vulnerability (severity %s) - see %s%c",
239                 scriptdir, $2, $1,
240                 $1,
241                 $3,
242                 34, $1, $3, $4, $5, $6, 34);
243         system(cmd);
244 }
245 ' "$vuls"