summaryrefslogtreecommitdiffhomepage
path: root/once/get_updates.sh
blob: 7e779dba9a4e29d2739ef29e75be59c0d5fcb7a3 (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
#!/bin/sh

set -eu

trap update_failure 1 2 EXIT

# before we begin...
mb_path="$PATH"
mb_script="$0"
mb_success=no
mb_obtain=no
mb_state=true
mb_dlopt="${1:-}"

error_msg()
{
	printf '%s\n' "$@" >&2
}

warning_msg()
{
	printf '%s\n' "$@" >&2
}

update_failure()
{
	if [ _$mb_success = _yes ]; then
		return 0
	fi

	printf 'update info: exiting due to an error.\n' >&3

	exit 2
}


update_needed()
{
	trap '' EXIT
	mb_success=yes
	exit 1
}


update_success()
{
	trap '' EXIT
	mb_success=yes
	exit 0
}

obtain_remote_file()
{
	pathname="$1"

	case "$mb_vendor" in
		/ )
			;;

		/* )
			cp -p "${mb_vendor}/${pathname}" \
				"${pathname}"
			;;

		https://* )
			wget "${mb_vendor}/${pathname}"       \
				--output-document="${pathname}"  \
				--no-check-certificate
				;;

		* )
			error_msg "Invalid prefix in path argument ${pathname}"
			update_failure
			;;
	esac
}

# logging
exec 3>> /updates/update.log

# previous state
if [ -f /updates/update.pending ]; then
	rm /updates/update.pending
	update_needed
fi

# vendor server location
mb_vendor=$(cat /etc/vendor.host)

# obtain list of advertised updates
obtain_remote_file /updates/updates.sha256
mb_tarballs=$(cut -d' ' -f3 /updates/updates.sha256)

# simple argument parsing
if [ "${mb_dlopt}" = '--obtain-tarballs' ]; then
	mb_obtain=yes
fi

# compare against local state
for tarball in ${mb_tarballs:-}; do
	printf 'checking local status of %s...\n' $tarball >&3

	if ! [ -f /updates/$tarball ]; then
		if [ -f /tarballs/$tarball ]; then
			if ! [ -f /tarballs/$tarball.sha256 ]; then
				sha256sum /tarballs/$tarball > /tarballs/$tarball.sha256
			fi

			mb_remotesig=$(grep $tarball /updates/updates.sha256 | cut -d' ' -f1)
			mb_localsig=$(cat /tarballs/$tarball.sha256 | cut -d' ' -f1)

			printf '\tremote signature: %s\n' $mb_remotesig >&3
			printf '\tcached signature: %s\n' $mb_localsig >&3

			if [ $mb_localsig != $mb_remotesig ]; then
				mb_state=false

				printf '\tsignatures do not match, download needed.\n' >&3

				if [ $mb_obtain = no ]; then
					update_needed
				else
					mb_needed=yes
				fi
			else
				printf '\tsignatures match, installed tarball is already up-to-date.\n' >&3
				mb_needed=no
			fi
		else
			printf '\t/updates/%s does not exist, download needed.\n' $tarball >&3

			if [ $mb_obtain = no ]; then
				update_needed
			else
				mb_needed=yes
			fi
		fi
	else
		mb_state=false

		printf '\t/updates/%s found, checking signatures...\n' $tarball >&3

		if ! [ -f /updates/$tarball.sha256 ]; then
			sha256sum /updates/$tarball > /updates/$tarball.sha256
		fi

		mb_remotesig=$(grep $tarball /updates/updates.sha256 | cut -d' ' -f1)
		mb_localsig=$(cat /updates/$tarball.sha256 | cut -d' ' -f1)

		printf '\tremote signature: %s\n' $mb_remotesig >&3
		printf '\tlocal  signature: %s\n' $mb_localsig >&3

		if [ $mb_localsig != $mb_remotesig ]; then
			printf '\tsignatures do not match, download needed.\n' >&3

			if [ $mb_obtain = no ]; then
				update_needed
			else
				mb_needed=yes
			fi
		else
			printf '\tsignatures match, local tarball is already up-to-date.\n' >&3
			mb_needed=no
		fi
	fi

	if [ $mb_needed = yes ]; then
		printf '\tattempting to download %s...\n' ${mb_vendor}/updates/$tarball >&3

		obtain_remote_file /updates/$tarball
		sha256sum /updates/$tarball > /updates/$tarball.sha256

		mb_remotesig=$(grep $tarball /updates/updates.sha256 | cut -d' ' -f1)
		mb_localsig=$(cat /updates/$tarball.sha256 | cut -d' ' -f1)

		printf '\tremote signature: %s\n' $mb_remotesig >&3
		printf '\tlocal  signature: %s\n' $mb_localsig >&3

		if [ $mb_localsig != $mb_remotesig ]; then
			printf 'signatures do not match, aborting.\n' >&3
			update_failure
		else
			printf '\t/local tarball is now up-to-date.\n' >&3
		fi
	fi

	printf '\n' >&3
done

# already up-to-date?
if [ "${mb_dlopt}" = '--check-state' ]; then
	if [ $mb_state = true ]; then
		update_success
	else
		update_needed
	fi
fi

# status
touch /updates/update.pending

# all done
update_success