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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
#!/bin/sh
# Copyright (c) 2004-2015 Electric Sheep Fencing, LLC. All rights reserved.
#
# 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 acknowledgment:
# "This product includes software developed by the pfSense Project
# for use in the pfSense® software distribution. (http://www.pfsense.org/).
#
# 4. The names "pfSense" and "pfSense Project" must not be used to
# endorse or promote products derived from this software without
# prior written permission. For written permission, please contact
# coreteam@pfsense.org.
#
# 5. Products derived from this software may not be called "pfSense"
# nor may "pfSense" appear in their names without prior written
# permission of the Electric Sheep Fencing, LLC.
#
# 6. Redistributions of any form whatsoever must retain the following
# acknowledgment:
#
# "This product includes software developed by the pfSense Project
# for use in the pfSense software distribution (http://www.pfsense.org/).
#
# THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
# EXPRESSED 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 pfSense PROJECT OR
# ITS CONTRIBUTORS 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.
usage() {
echo "Usage: $(basename ${0}) [-dy] [-u|-i PKG_NAME|-r PKG_NAME]" >&2
echo " -d - Turn on debug" >&2
echo " -h - Show this usage help" >&2
echo " -p FIFO - Write pkg progress to FIFO"
echo " -y - Consider yes as the answer for any possible interaction" >&2
echo "" >&2
echo "Following parameters are mutually exclusive:" >&2
echo " -i PKG_NAME - Install package PKG_NAME" >&2
echo " -r PKG_NAME - Remove package PKG_NAME" >&2
echo " -u - Update repository information" >&2
}
_echo() {
local _n=""
if [ "${1}" = "-n" ]; then
shift
_n="-n"
fi
if [ -z "${logfile}" ]; then
logfile=/dev/null
fi
echo ${_n} "${1}" | tee -a ${logfile}
}
_exec() {
local _cmd="${1}"
local _msg="${2}"
local _mute="${3}"
local _ignore_result="${4}"
local _stdout="${stdout}"
if [ -z "${_cmd}" -o -z "${_msg}" ]; then
return 1
fi
if [ "${_mute}" != "mute" ]; then
_stdout=''
fi
_echo -n ">>> ${_msg}... "
if [ -z "${_stdout}" ]; then
_echo ""
${_cmd} 2>&1 | tee -a ${logfile}
else
${_cmd} >${_stdout} 2>&1 | tee -a ${logfile}
fi
local _result=$?
if [ ${_result} -eq 0 -o -n "${_ignore_result}" ]; then
[ -n "${_stdout}" ] \
&& _echo "done."
return 0
else
[ -n "${_stdout}" ] \
&& _echo "failed."
return 1
fi
}
_exit() {
trap "-" 1 2 15 EXIT
if [ -n "${kernel_pkg}" ]; then
if [ "$(pkg query %k ${kernel_pkg})" = "0" ]; then
_exec "pkg lock ${kernel_pkg}" "Locking kernel package" mute ignore_result
fi
fi
if [ -f "${pid_file}" ]; then
rm -f ${pid_file}
fi
local _rc=${1:-"0"}
# If EVENT_PIPE is defined, GUI is calling
[ -n "${EVENT_PIPE}" ] \
&& _echo "__RC=${_rc}"
exit ${_rc}
}
pkg_upgrade_first_step() {
# figure out which kernel variant is running
kernel_pkg=$(pkg query %n $(pkg info pfSense-kernel-\*))
if [ -z "${kernel_pkg}" ]; then
_echo "ERROR: It was not possible to identify which pfSense kernel is installed"
_exit 1
fi
kernel_version_compare=$(compare_pkg_version ${kernel_pkg})
if [ "${kernel_version_compare}" = "<" ]; then
kernel_update=1
if [ "$(pkg query %k ${kernel_pkg})" = "1" ]; then
_exec "pkg unlock ${kernel_pkg}" "Unlocking kernel package" mute ignore_result
fi
elif [ "${kernel_version_compare}" = "=" ]; then
kernel_update=0
elif [ "${kernel_version_compare}" = ">" ]; then
_echo "ERROR: You are using a newer kernel version than remote repository"
_exit 1
else
_echo "ERROR: Error comparing pfSense kernel local and remote versions"
_exit 1
fi
# XXX find a samrter way to do it
l=$(pkg upgrade -nq | wc -l)
if [ ${l} -eq 1 ]; then
_echo "Your packages are up to date"
_exit 0
fi
if [ -z "${yes}" ]; then
# Show user which packages are going to be upgraded
pkg upgrade -nq 2>&1 | tee -a ${logfile}
_echo ""
if [ ${kernel_update} -eq 1 ]; then
_echo "**** WARNING ****"
_echo "Reboot will be required!!"
fi
_echo -n "Proceed with upgrade? (y/N) "
read answer
if [ "${answer}" != "y" ]; then
_echo "Aborting..."
_exit 0
fi
fi
_echo ">>> Downloading packages..."
if ! pkg upgrade -F 2>&1 | tee -a ${logfile}; then
_echo "ERROR: It was not possible to download packages"
_exit 1
fi
# Mark firmware subsystem dirty
touch ${firmwarelock}
# First upgrade kernel and reboot
if [ ${kernel_update} -eq 1 ]; then
_exec "pkg upgrade ${kernel_pkg}" "Upgrading pfSense kernel"
touch ${upgrade_in_progress}
_echo "Rebooting..."
/etc/rc.reboot
fi
}
pkg_upgrade_second_step() {
_echo "Upgrading necessary packages..."
if ! pkg upgrade 2>&1 | tee -a ${logfile}; then
_echo "ERROR: An error occurred when upgrade was running..."
_exit 1
fi
_exec "pkg autoremove" "Removing unnecessary packages" mute ignore_result
_exec "pkg clean" "Cleanup pkg cache" mute ignore_result
# cleanup caches
rm -f ${upgrade_in_progress}
rm -f ${firmwarelock}
}
pkg_update() {
local _run_update=1
unset _force
if [ "${1}" = "force" ]; then
local _force=1
fi
if [ -z "${_force}" -a -f ${last_update_file} ]; then
local _last_update=$(head -n 1 ${last_update_file})
# Verify if content contain only numbers
if echo "${_last_update}" | grep -E -q '^[0-9]+$'; then
local _now=$(date +%s)
# Only run update hourly, and if last update is in the future
[ ${_now} -gt ${_last_update} -a $((${_now} - ${_last_update})) -le $((60 * 60)) ] \
&& unset _run_update
fi
fi
[ -z "${_run_update}" ] \
&& return 0
_exec "pkg update" "Updating repositories" mute
date +%s > ${last_update_file}
}
pkg_upgrade() {
pkg_update
unset need_reboot
if [ ! -f "${upgrade_in_progress}" ]; then
if [ -f "${logfile}" ]; then
rm -f ${logfile}
fi
pkg_upgrade_first_step
need_reboot=1
fi
pkg_upgrade_second_step
if [ -n "${need_reboot}" ]; then
_echo "Rebooting..."
/etc/rc.reboot
fi
}
is_pkg_installed() {
local _pkg_name="${1}"
pkg info -e ${_pkg_name}
return $?
}
compare_pkg_version() {
local _pkg_name="${1}"
if ! is_pkg_installed ${_pkg_name}; then
echo '!'
return -1
fi
local _lver=$(pkg query %v ${_pkg_name})
if [ -z "${_lver}" ]; then
_echo "ERROR: It was not possible to determine ${_pkg_name} local version"
_exit 1
fi
local _rver=$(pkg rquery %v ${_pkg_name})
if [ -z "${_rver}" ]; then
_echo "ERROR: It was not possible to determine ${_pkg_name} remote version"
_exit 1
fi
local _version=$(pkg version -t ${_lver} ${_rver})
if [ $? -ne 0 ]; then
_echo "ERROR: Error comparing ${_pkg_name} local and remote versions"
_exit 1
fi
echo ${_version}
return 0
}
pkg_install() {
local _pkg_name="${1}"
if [ -z "${_pkg_name}" ]; then
_echo "ERROR: Blank package name"
_exit 1
fi
pkg_update
if is_pkg_installed ${_pkg_name}; then
local _cversion=$(compare_pkg_version ${_pkg_name})
if [ "${_cversion}" = "=" ]; then
_echo "Package ${_pkg_name} is up to date"
_exit 0
elif [ "${_cversion}" = ">" ]; then
_echo "Installed ${_pkg_name} version is newer than remote"
_exit 0
fi
local _cmd="upgrade"
local _msg="Upgrading"
else
local _cmd="install"
local _msg="Installing"
fi
_exec "pkg ${_cmd} ${_pkg_name}" "${_msg} ${_pkg_name}"
_exec "pkg clean" "Cleaning up cache" mute ignore_result
}
pkg_delete() {
local _pkg_name="${1}"
if [ -z "${_pkg_name}" ]; then
_echo "ERROR: Blank package name"
_exit 1
fi
if ! is_pkg_installed ${_pkg_name}; then
_echo "ERROR: Package ${_pkg_name} is not installed"
_exit 1
fi
_exec "pkg delete ${_pkg_name}" "Removing ${_pkg_name}"
_exec "pkg autoremove" "Removing stale packages" mute ignore_result
}
pid_file="/var/run/$(basename $0).pid"
last_update_file="/var/run/$(basename $0)-last-update"
logfile=/cf/conf/upgrade_log.txt
stdout='/dev/null'
# File used to detect second call, after kernel update and reboot
upgrade_in_progress="/cf/conf/upgrade_in_progress"
# pkg should not ask for confirmations
export ASSUME_ALWAYS_YES=true
# Disable automatic update
export REPO_AUTOUPDATE=false
unset yes
unset progress_fifo
unset action
unset action_pkg
while getopts di:hp:r:uy opt; do
case ${opt} in
d)
stdout=''
;;
i)
if [ -n "${action}" ]; then
usage
_exit 1
fi
action="install"
action_pkg="${OPTARG}"
;;
h)
usage
_exit 0
;;
p)
progress_fifo="${OPTARG}"
;;
r)
if [ -n "${action}" ]; then
usage
_exit 1
fi
action="delete"
action_pkg="${OPTARG}"
;;
u)
if [ -n "${action}" ]; then
usage
_exit 1
fi
action="update"
;;
y)
yes=1
;;
*)
usage
_exit 1
;;
esac
done
# Set default action when no parameter is set
: ${action:="upgrade"}
if pgrep -qF ${pid_file} >/dev/null 2>&1; then
echo "Another instance is already running... Aborting!"
_exit 1
fi
echo $$ > ${pid_file}
trap _exit 1 2 15 EXIT
if [ -n "${progress_fifo}" ]; then
if [ ! -e "${progress_fifo}" ]; then
mkfifo ${progress_fifo}
fi
if [ ! -p "${progress_fifo}" ]; then
_echo "ERROR: ${progress_fifo} is not a FIFO"
_exit 1
fi
export EVENT_PIPE="${progress_fifo}"
fi
case "${action}" in
upgrade)
pkg_upgrade
;;
update)
pkg_update force
;;
install)
pkg_install ${action_pkg}
;;
delete)
pkg_delete ${action_pkg}
;;
*)
_echo "ERROR: Invalid action!"
_exit 1
esac
_exit 0
|