summaryrefslogtreecommitdiff
path: root/pkgtools/pkg_install/files/audit-packages/download-vulnerability-list.sh.in
blob: 949af37c337fb889822b3416b70eaffbcb07c366 (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/sh

# $NetBSD: download-vulnerability-list.sh.in,v 1.1 2007/07/14 20:17:10 adrianp Exp $
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#	This product includes software developed by Alistair Crooks
#	for the NetBSD project.
# 4. The name of the author may not be used to endorse or promote
#    products derived from this software without specific prior written
#    permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

: ${PKGVULNDIR="@pkgdbdir@"}
: ${COMPRESS_TYPE="gzip"}
: ${FETCH_CMD=@ftp@}
: ${FETCH_ARGS=""}
: ${FETCH_PROTO=ftp}

AP=@prefix@/sbin/audit-packages
NEW_VUL_LIST=pkg-vulnerabilities.$$
EXIST_VUL_LIST=pkg-vulnerabilities
FETCH_PATH="ftp.NetBSD.org/pub/NetBSD/packages/vulns"
_CONF_FILE="@sysconfdir@/audit-packages.conf"
COMPRESS_TOOL=""

usage()
{
	argv0="${1##*/}"
	cat <<EOF
$2
Usage: $argv0 [-s] [-c config-file]
	-s : Verify the signature on the downloaded file.
	-c : Specify a custom configuration file to use.
EOF
	exit 1
}

verify=no
custom_conf=no
conf_found=no
neednew=no

while [ $# -gt 0 ]; do
	case "$1" in
	-s)
		verify=yes
		;;
	-c)
		custom_conf=yes
		local_conf="$2"
		;;
	*)
		usage "$0" "Unknown option $1"
	esac
	shift
done

#  generic conf file handler
if [ -r ${_CONF_FILE} ]; then
	conf_found=yes
fi

# see if the user wants us to use a custom config file
if [ "x${custom_conf}" = "xyes" ]; then
	if [ -r ${local_conf} ]; then
		conf_found=yes
		_CONF_FILE=${local_conf}
	fi
fi

# only do the following if we have found a config file to use
if [ "x${conf_found}" = "xyes" ]; then
	if [ -r ${_CONF_FILE} ]; then
		echo "Reading settings from ${_CONF_FILE}"
		. ${_CONF_FILE}
	fi
fi

# setup the compression type
case "${COMPRESS_TYPE}" in
bzip2)		COMPRESS_EXTN=.bz2
		compressed=yes
		;;
gzip)		COMPRESS_EXTN=.gz
		compressed=yes
		;;
none)		COMPRESS_EXTN=""
		compressed=no
		;;
*)		echo "***ERROR*** Unknown COMPRESS_TYPE specified - Only bzip2 and gzip are currently supported."
		exit 1
		;;
esac

# setup the compression tool and arguments
if [ "x${compressed}" = "xyes" ]; then
	if [ "x${COMPRESS_TYPE}" = "xgzip" -a "x${COMPRESS_TOOL}" = "x" ]; then
		COMPRESS_TOOL="@gzcat@"
	fi

	if [ "x${COMPRESS_TYPE}" != "xgzip" -a "x${COMPRESS_TOOL}" = "x" ]; then
		echo "***ERROR*** A non-default COMPRESS_TYPE has been specified without a COMPRESS_TOOL"
		exit 1
	fi
fi

VUL_SOURCE="${FETCH_PROTO}://${FETCH_PATH}/pkg-vulnerabilities${COMPRESS_EXTN}"

if [ ! -d ${PKGVULNDIR}/. ]; then
	echo "Creating ${PKGVULNDIR}"

	/bin/mkdir ${PKGVULNDIR}
	if [ ! -d  ${PKGVULNDIR} ]; then
		echo "***ERROR*** Can't create: ${PKGVULNDIR}"
		exit 1
	fi
fi

if [ ! "x${FETCH_PROTO}" = "xhttp" -a ! "x${FETCH_PROTO}" = "xftp" ]; then
	echo "***ERROR*** Unknown FETCH_PROTO specified - Only http and ftp are currently supported."
	exit 1
fi

cd ${PKGVULNDIR}
utility=`basename "${FETCH_CMD}"`
case "${utility}" in
curl|fetch|ftp)	${FETCH_CMD} ${FETCH_ARGS} \
			-o ${NEW_VUL_LIST}${COMPRESS_EXTN} ${VUL_SOURCE} ;;
wget)		${FETCH_CMD} ${FETCH_ARGS} \
			-O ${NEW_VUL_LIST}${COMPRESS_EXTN} ${VUL_SOURCE} ;;
*)		echo "Unknown fetch command - please use send-pr to send in support for your fetch command" 1>&2
		exit 1
		;;
esac

# see if we got a file
if [ ! -f "${NEW_VUL_LIST}${COMPRESS_EXTN}" ]; then
	echo "***ERROR*** Download of vulnerabilities file failed" 1>&2
	exit 1
fi

# decompress the downloaded file and delete the download
if [ "x${compressed}" = "xyes" ]; then
	${COMPRESS_TOOL} ${NEW_VUL_LIST}${COMPRESS_EXTN} > ${NEW_VUL_LIST}
	/bin/rm -f ${NEW_VUL_LIST}${COMPRESS_EXTN}
fi

# compare the old and new files to see if there's a difference
if [ -f ${EXIST_VUL_LIST} ]; then
	exist_hash=`${AP} -g ${EXIST_VUL_LIST}`
	new_hash=`${AP} -g ${NEW_VUL_LIST}`

	if [ "x${exist_hash}" != "x${new_hash}" ]; then
		neednew=yes
	else
		echo "No change from existing package vulnerabilities file"
		/bin/rm -f ${NEW_VUL_LIST}
		exit 0
	fi
else
	neednew=yes
fi

# check the hash and/or sig on the new file
if [ "x${verify}" = "xyes" ]; then
	${AP} -s -h ${NEW_VUL_LIST}
else
	${AP} -h ${NEW_VUL_LIST}
fi

ec=$?;

if [ $ec -ne 0 ]; then
	echo "***ERROR*** Failed to verify the newly downloaded vulnerabilities file" 1>&2
	/bin/rm -f ${NEW_VUL_LIST}
	exit 1
fi

# move the new file into position
echo "Package vulnerabilities file has been updated"
/bin/chmod a+r ${NEW_VUL_LIST}
/bin/mv -f ${NEW_VUL_LIST} ${EXIST_VUL_LIST}

exit 0