#!/bin/sh # Install the WebUI from GitHub over the copy the firmware already ships. # # The camera boots a read-only squashfs, pivots it to /rom and mounts a # writable overlay on top, so every file written here becomes an overlay copy # that hides the firmware's own file underneath it. Two consequences shape this # script: # # * an overlay copy outlives the firmware upgrade that would have replaced # it, and goes on hiding the newer file that upgrade delivered. So only # files that genuinely differ from the firmware's are left in the overlay, # and --restore takes even those away. # * deleting through the merged path (a plain `rm /var/www/x`) writes a # whiteout that hides the firmware's copy for good. So nothing here removes # a WebUI file that way; removals are made in the overlay directory itself. # # Nothing on the camera is touched until the download has been fetched, # verified and unpacked, so a failed or interrupted download leaves the running # WebUI exactly as it was. # # This file is NOT on the camera. What the image carries as # /usr/sbin/updatewebui is sbin/updatewebui-fetch, a stub that downloads this # script -- from the branch being installed, where one was named -- caches it # in /etc/webui and runs it. Two reasons, and the second is the better one: # # * the rootfs is a fixed-size squashfs partition and several boards are at # the edge of it. This script is 48 KB of a 5120 KB partition that an # 8 MB gk7202v300 had 12 KB of headroom in. # * a script that installs itself is always one release behind: the copy # running an install was the one the PREVIOUS install left, so a fix to # the installer took effect on the run after the one that fetched it -- # exactly backwards for the run bringing the fix down. Fetching first # makes the running copy the newest by construction. # # Nothing is lost by it: this script's only purpose is to download a tree from # GitHub, so a camera that cannot reach GitHub could not use it either way. scr_name=updatewebui scr_version=10 REPO=openipc/majestic-webui STATE_DIR=/etc/webui MANIFEST=$STATE_DIR/updatewebui.manifest DIRS=$STATE_DIR/updatewebui.dirs KEEP_BACKUPS=3 # What the firmware's own buildroot package installs, and where it puts it. # Kept in step with tools/build-dist.sh, which is what buildroot consumes. PAYLOAD='www:/var/www sbin:/usr/sbin bin:/usr/bin' # Everything this project has ever installed outside /var/www. A camera updated # by the manifest-less version of this script left no record of what it copied # there, and --restore exists largely for those, so it falls back to this list. LEGACY_SHARED='/usr/sbin/openwall /usr/sbin/setnetwork /usr/sbin/telegram /usr/sbin/updatewebui /usr/bin/ntfy.sh' branch= zip_url= resolved_sha= mode=install dry_run=0 do_backup=1 die() { echo "$scr_name: $1" >&2 exit 1 } print_usage() { cat <&2 echo "Try '$scr_name --help'." >&2 exit 1 ;; *) branch=$1 ;; esac shift done [ -n "$branch" ] || branch=master # fetch_tree fills zip_url in from the branch, so what the caller actually # asked for has to be kept apart from it: write_version reports the source, and # must report the one that was requested rather than the one derived from it. zip_url_given=$zip_url # Not over plain http: what arrives is unpacked and run as root, and anyone on # the path could swap it. https, or copy the zip here and use file://, which # is the offline route anyway. case $zip_url in [Hh][Tt][Tt][Pp]://*) die "refusing a plain http:// archive; use https:// or file://" ;; esac # Where a write to / physically lands, and where the firmware's own copy of a # file lives. /init mounts the overlay two ways depending on the kernel: the # modern "overlay" with upperdir=/overlay/root, and the 3.10 out-of-tree # "overlayfs" with upperdir=/overlay. Both pivot the squashfs to /rom. Without # an overlay (NFS or RAM root) there is nothing to shadow and no upper layer to # clean, and the script degrades to a plain install. upper=$(awk '$2 == "/" && ($3 == "overlay" || $3 == "overlayfs") { n = split($4, o, ",") for (i = 1; i <= n; i++) if (o[i] ~ /^upperdir=/) { print substr(o[i], 10); exit } }' /proc/mounts) [ -n "$upper" ] && [ -d "$upper" ] || upper= lower=/rom { [ -n "$upper" ] && [ -d "$lower/var/www" ]; } || lower= # lite/ultimate/fpv - which of the release's files this firmware gets. An # unknown or missing value reads as a standard build, the same default # buildroot has. variant=$(sed -n 's/^BUILD_OPTION=//p' /etc/os-release 2>/dev/null | tr -d '"') # OpenIPC/builder renamed the fpv variant wfbng, after the wfb-ng it carries; # it gets the same files, and the firmware's majestic-webui.mk treats the two # names alike. [ "$variant" = wfbng ] && variant=fpv # The SMS page and its two scripts are a build option, set by the device # profiles of boards with a cellular modem, and a camera cannot read its own # build options -- but the image records the answer, because a build without # them has no /usr/sbin/modem-at. So what the image carries decides, and an # update never puts SMS back on a camera whose firmware left it out. With no # overlay the root is the earlier install itself, and it keeps what it has. sms=0 [ -e "$lower/usr/sbin/modem-at" ] && sms=1 # The FPV settings page is a build option too, on by default for the fpv and # wfbng variants but settable by a profile either way, so BUILD_OPTION is no # longer the whole answer. The image carries p/fpv_common.cgi exactly when it # carries the page -- under either of the page's two names -- so that is the # witness. fpv=0 [ -e "$lower/var/www/cgi-bin/p/fpv_common.cgi" ] && fpv=1 # The PTZ controls follow the motor drivers the same way: firmware keeps them # only where gpio-motors or the majestic-af plugin is built in, and those are # what moves a motor, so they are the witness here too -- with /usr/bin/motor, # the third backend's helper, which no package installs but a board may. ptz=0 { [ -e "$lower/usr/bin/gpio-motors" ] || [ -e "$lower/usr/lib/majestic-af.so" ] || [ -e "$lower/usr/bin/motor" ]; } && ptz=1 # The Counting page is a build option as well, on by default where the image # carries an object-detection model (xmnpu-models for the NPU, cpu-models for # the CPU) -- counting runs on that detector's boxes, so an image without one # has nothing to count with. The page is its own witness, as the FPV page is. counting=0 [ -e "$lower/var/www/cgi-bin/counting.cgi" ] && counting=1 tmp=$(mktemp -d) || die "cannot create a temporary directory" # The staged directory holds the script this shell is reading, which on Linux # survives its own unlink for as long as the shell holds it open. cleanup() { rm -rf "$tmp" return 0 } trap cleanup EXIT INT TERM # sync + drop_caches because the kernel keeps serving a cached merged view of a # directory whose upper layer was edited underneath it. On the 4.x "overlay" # this settles it; on the 3.10 "overlayfs" only a reboot does, which is what # verify_view() falls back to telling the user. settle_view() { sync [ -w /proc/sys/vm/drop_caches ] && echo 3 >/proc/sys/vm/drop_caches return 0 } # $1 is a md5sum-format list of what every path should now hold, $2 an # optional list of paths that should no longer exist at all. Anything that # disagrees means the merged view has not caught up with the overlay, which the # kernel makes no promise about while the overlay is mounted. verify_view() { : >"$tmp/stale" [ -s "$1" ] && md5sum -c "$1" 2>/dev/null | sed -n 's/: FAILED.*$//p' >>"$tmp/stale" [ -n "${2:-}" ] && [ -s "$2" ] && while read -r d; do [ -e "$d" ] && echo "$d"; done <"$2" >>"$tmp/stale" [ -s "$tmp/stale" ] || return 0 echo echo "The kernel is still serving a cached view of these files:" sed 's/^/ /' "$tmp/stale" if grep -qxF "/usr/sbin/$scr_name" "$tmp/stale"; then echo "One of them is this script, whose own installed copy this run has" echo "just changed; a running program keeps the view of its own file alive." fi echo "Flash is already correct. Reboot the camera to pick the changes up." return 0 } fetch_tree() { if [ -n "$zip_url" ]; then source_name=$zip_url else # Resolve the branch to its exact commit and fetch THAT commit's # archive, so the tree installed and the version recorded for it are the # same revision by construction: a branch that advances between the # fetch and the stamp cannot leave them naming different trees. The # branch zip is the fallback when the commit cannot be resolved # (offline, rate limited), paired with a branch-only stamp. resolved_sha=$(curl -fsSL --connect-timeout 10 "https://api.github.com/repos/$REPO/commits/$branch" 2>/dev/null | grep -m1 '"sha"' | sed -n 's/.*"sha": *"\([0-9a-f]\{40\}\).*/\1/p') if [ -n "$resolved_sha" ]; then zip_url="https://github.com/$REPO/archive/$resolved_sha.zip" else zip_url="https://github.com/$REPO/archive/refs/heads/$branch.zip" fi source_name=$branch fi echo "Fetch $zip_url" # The http refusal above holds through redirects too: https, or the # file:// an offline camera is updated with, and a redirect only to https. curl -fsSL --proto =https,file --proto-redir =https --connect-timeout 30 -o "$tmp/webui.zip" "$zip_url" || die "download failed, nothing has been changed" # Unpacking into the scratch directory is the whole validation: it rejects # what is not a zip and what is a truncated one, and it needs no option # beyond the four busybox unzip has always had. mkdir -p "$tmp/x" unzip -qo -d "$tmp/x" "$tmp/webui.zip" >/dev/null 2>&1 || die "downloaded file is not a usable zip archive, nothing has been changed" set -- "$tmp/x"/* { [ $# -eq 1 ] && [ -d "$1" ]; } || die "expected one directory in the archive, nothing has been changed" src=$1 [ -f "$src/www/cgi-bin/p/common.cgi" ] || die "the archive does not hold a WebUI tree, nothing has been changed" # busybox unzip applies the caller's umask, which is 0077 on OpenIPC, so # everything would land root-only. Restore the modes the firmware ships. find "$src" -type d -exec chmod 0755 {} + find "$src" -type f -exec chmod 0644 {} + find "$src/www" -name '*.cgi' -exec chmod 0755 {} + for d in sbin bin; do [ -d "$src/$d" ] && chmod 0755 "$src/$d"/* done return 0 } # write: mode, source and destination of every file that has to be installed # drop: overlay copies to delete because the firmware's own file is identical # prune: overlay copies to delete because this release no longer has the file # keep: every destination this release owns # want: md5sum-format list of what the install should leave behind build_plan() { for f in write drop prune keep want local atrisk skipped newdirs; do : >"$tmp/$f"; done for pair in $PAYLOAD; do sub=${pair%%:*} root=${pair#*:} [ -d "$src/$sub" ] || continue (cd "$src/$sub" && find . ! -type d) | sed 's|^\./||' | while read -r rel; do s=$src/$sub/$rel d=$root/$rel # The release carries every variant's files; the firmware # does not. Mirror the fixup lists in buildroot's # majestic-webui.mk (kept in step with them): a standard # build strips the FPV pages, an FPV build strips the # notifier helpers, and only a build that asked for SMS # keeps it (see $sms). A skipped path never enters keep, # so a stale overlay copy of it is pruned below. # # fpv-wfb.cgi is the pre-rename name of wfb.cgi. Both # are listed so this script installs an older tree and # a newer one correctly -- which it must, since --url # and --branch will both hand it either. That used to # need a version bump as well, because the copy running # an install was the one the PREVIOUS install left, so # the run delivering a new name was handled by a script # that had never heard of it and wfb.cgi landed on a # standard build -- measured on the lite lab camera, # not reasoned. Fetching the installer first is what # retired that whole class of fault. case $sub/$rel in sbin/$scr_name) # The installer is downloaded, never # installed -- but only where this tree # actually carries the stub that downloads # it. A tree from before the split, or one # already reduced to the shipped shape (the # stub sitting at this very path), has # nothing else to put here, and installing # what is in front of it is what those two # cases need. Not counted as skipped: that # line means "for another firmware variant". if [ -f "$src/sbin/$scr_name-fetch" ]; then continue fi ;; sbin/$scr_name-fetch) d=$root/$scr_name ;; www/cgi-bin/wfb.cgi | www/cgi-bin/fpv-wfb.cgi | www/cgi-bin/p/fpv_common.cgi | \ www/a/wfb.js) if [ "$fpv" != 1 ]; then echo "$d" >>"$tmp/skipped" continue fi ;; sbin/telegram | sbin/max | sbin/openwall) if [ "$variant" = fpv ]; then echo "$d" >>"$tmp/skipped" continue fi ;; www/cgi-bin/sms.cgi | www/cgi-bin/j/sms.cgi | www/a/sms.js | \ www/a/sms-pdu.js | sbin/sms-forward | sbin/modem-at) if [ "$sms" != 1 ]; then echo "$d" >>"$tmp/skipped" continue fi ;; www/cgi-bin/counting.cgi | www/a/counting.js | www/a/counting-data.js) if [ "$counting" != 1 ]; then echo "$d" >>"$tmp/skipped" continue fi ;; www/a/preview-ptz.js | www/a/ptz-stick.js | www/a/ptz-centre.js | \ www/a/ptz-follow.js | www/a/ptz-speed.js | www/a/ptz-orient.js | \ www/a/af-state.js | \ www/cgi-bin/j/ptz.cgi) if [ "$ptz" != 1 ]; then echo "$d" >>"$tmp/skipped" continue fi ;; www/cgi-bin/p/motor.cgi) # Never absent, only emptied: live.cgi pulls it in # with <%in %>, which haserl opens while parsing the # page, before any shell around it runs, so a missing # file breaks the Live page whatever ptz_control says. # The firmware ships the same empty stub, so on such an # image this matches /rom and writes nothing. if [ "$ptz" != 1 ]; then : >"$tmp/motor.cgi.stub" chmod "$(stat -c %a "$s")" "$tmp/motor.cgi.stub" s=$tmp/motor.cgi.stub fi ;; esac echo "$d" >>"$tmp/keep" md5sum "$s" | sed "s| .*| $d|" >>"$tmp/want" # The firmware already has this exact file: an overlay # copy of it would buy nothing and hide the next one. if [ -n "$lower" ] && cmp -s "$s" "$lower$d"; then if [ -e "$upper$d" ]; then # Release and firmware agree here, so an overlay # copy that differs can only be an edit made on # this camera. Whiteouts and other non-files # hold nothing worth saving. if [ -f "$upper$d" ] && ! cmp -s "$upper$d" "$s"; then echo "$d" >>"$tmp/atrisk" [ -s "$MANIFEST" ] || echo "$d" >>"$tmp/local" fi echo "$upper$d" >>"$tmp/drop" fi continue fi if cmp -s "$s" "$d" 2>/dev/null; then [ "$(stat -c %a "$d")" = "$(stat -c %a "$s")" ] && continue # Same content, wrong mode: rewritten, nothing lost. else echo "$d" >>"$tmp/atrisk" # The rootfs is read-only, so differing content that # lives in the upper layer was put there on this # camera and exists nowhere else. { [ ! -s "$MANIFEST" ] && [ -n "$upper" ] && [ -f "$upper$d" ]; } && echo "$d" >>"$tmp/local" fi echo "$(stat -c %a "$s") $s $d" >>"$tmp/write" done done # /var/www belongs to the WebUI alone, so anything left in the overlay # that this release does not have is stale and goes. This is also what # clears the whiteouts an older updatewebui left behind, which is why the # search is for anything that is not a directory rather than for files. if [ -n "$upper" ] && [ -d "$upper/var/www" ]; then [ -s "$MANIFEST" ] && sed 's/^[0-9a-f]* //' "$MANIFEST" >"$tmp/manifest.paths" find "$upper/var/www" ! -type d | while read -r u; do d=${u#$upper} grep -qxF "$d" "$tmp/keep" && continue echo "$u" >>"$tmp/prune" echo "$d" >>"$tmp/atrisk" # A stale file that is not simply the firmware's own content # is work done on this camera. Paths the manifest records are # judged against the manifest below instead. [ -f "$u" ] || continue if [ -s "$MANIFEST" ]; then grep -qxF "$d" "$tmp/manifest.paths" && continue fi [ -n "$lower" ] && cmp -s "$u" "$lower$d" && continue echo "$d" >>"$tmp/local" done fi # /usr/sbin and /usr/bin are shared with the rest of the firmware, so only # the files a previous run of this script installed may be removed there. if [ -s "$MANIFEST" ]; then while read -r _ d; do case $d in /var/www/*) continue ;; esac grep -qxF "$d" "$tmp/keep" && continue if [ -n "$upper" ] && [ -e "$upper$d" ]; then echo "$upper$d" >>"$tmp/prune" elif [ -z "$upper" ] && [ -e "$d" ]; then echo "$d" >>"$tmp/prune" else continue fi echo "$d" >>"$tmp/atrisk" done <"$MANIFEST" fi # Directories the writes will bring into existence in the upper layer. # They are invisible to the manifest - no content to hash - so they get a # record of their own, which is what lets --restore offer to take the # empty shells away again in a shared tree like /usr/sbin, where an # unrecorded directory cannot be told from somebody else's. if [ -n "$upper" ]; then cut -d' ' -f3 "$tmp/write" | while read -r w; do w=${w%/*} while [ -n "$w" ] && [ ! -e "$upper$w" ]; do echo "$w" >>"$tmp/newdirs" w=${w%/*} done done sort -u "$tmp/newdirs" >"$tmp/newdirs.u" && mv "$tmp/newdirs.u" "$tmp/newdirs" fi # Anything that no longer matches the manifest was edited on the camera. # Without a manifest the overlay itself is the record: the rootfs is # read-only, so every local edit necessarily lives in the upper layer, # which is what the hooks above compare against the release and /rom. if [ -s "$MANIFEST" ]; then md5sum -c "$MANIFEST" 2>/dev/null | sed -n 's/: FAILED.*$//p' | while read -r d; do [ -f "$d" ] && echo "$d"; done >>"$tmp/local" fi sort -u "$tmp/local" >"$tmp/local.u" && mv "$tmp/local.u" "$tmp/local" # A file can only be lost with the run that overwrites or removes it, so # that is the whole of what is worth archiving. The manifest answers a # different question - is this still what an install put here - and a # path can fail it while already holding exactly what this release # ships, which is what anything deployed onto the camera by hand between # two installs looks like. Calling those "to be replaced" and archiving # a copy of a file nobody is going to touch is how the state directory # fills up with archives of a tree that never changed. # busybox grep reads an empty -f file as "match everything", where GNU # grep matches nothing, so a run with nothing at risk would keep the # whole list rather than empty it. if [ -s "$tmp/local" ]; then if [ -s "$tmp/atrisk" ]; then grep -xF -f "$tmp/atrisk" "$tmp/local" >"$tmp/local.r" mv -f "$tmp/local.r" "$tmp/local" else : >"$tmp/local" fi fi return 0 } count() { wc -l <"$1" | tr -d ' '; } report_plan() { echo if [ -n "$upper" ]; then echo "Install $source_name over the WebUI this firmware ships" else echo "Install $source_name (no overlay is mounted, this rootfs is writable)" fi echo " $(count "$tmp/write") file(s) to write" [ -n "$upper" ] && echo " $(count "$tmp/drop") overlay copy(ies) to drop, the firmware's own file already matches" echo " $(count "$tmp/prune") stale entry(ies) to remove" [ -s "$tmp/skipped" ] && echo " $(count "$tmp/skipped") file(s) this firmware leaves out, skipped" [ -s "$tmp/newdirs" ] && echo " $(count "$tmp/newdirs") director(ies) to create" if [ -s "$tmp/local" ]; then echo if [ ! -s "$MANIFEST" ]; then echo "No record of an earlier install; overlay copies that differ" echo "from what this install leaves behind count as local edits." fi echo "Customizations, to be replaced or removed:" sed 's/^/ /' "$tmp/local" elif [ ! -s "$MANIFEST" ]; then echo if [ -n "$upper" ]; then echo "No record of an earlier install; overlay copies that differ" echo "from what this install leaves behind would count as local" echo "edits, and none were found." else echo "No record of an earlier install and no overlay to compare" echo "against, so local edits cannot be recognised this time and" echo "are replaced without a backup." fi fi if [ "$dry_run" = 1 ]; then echo [ -s "$tmp/write" ] && { echo "Write:"; cut -d' ' -f3 "$tmp/write" | sed 's/^/ /'; } [ -s "$tmp/drop" ] && { echo "Drop:"; sed "s|^$upper| |" "$tmp/drop"; } [ -s "$tmp/prune" ] && { echo "Remove:"; sed "s|^$upper| |" "$tmp/prune"; } fi return 0 } # Runs before anything is written, so refusing here still leaves the camera # untouched. An archive that failed is worth stopping for: the whole point of it # is that the run is about to replace work that exists nowhere else. backup_local() { [ -s "$tmp/local" ] || return 0 if [ "$do_backup" != 1 ]; then echo "Replacing $(count "$tmp/local") customization(s) unarchived (--no-backup)" return 0 fi mkdir -p "$STATE_DIR" || die "cannot create $STATE_DIR to archive the customizations, nothing has been changed" archive=$STATE_DIR/updatewebui-local-$(date +%Y%m%d-%H%M%S).tar.gz sed 's|^/||' "$tmp/local" >"$tmp/local.rel" # busybox tar has no -z, and a shell pipeline reports only its last command, # so the archive is built and compressed as two separately checked steps. tar cf "$tmp/local.tar" -C / -T "$tmp/local.rel" || die "cannot archive the customizations, nothing has been changed" gzip -9 -c "$tmp/local.tar" >"$archive" || { rm -f "$archive" die "cannot write $archive, nothing has been changed" } echo "Archived $(count "$tmp/local") customization(s) to $archive" # shellcheck disable=SC2012 ls -t "$STATE_DIR"/updatewebui-local-*.tar.gz 2>/dev/null | tail -n +$((KEEP_BACKUPS + 1)) | xargs rm -f 2>/dev/null return 0 } # One file, written to a temporary name and renamed into place, so a file that # is being executed right now - this script itself, on its way to /usr/sbin - is # replaced rather than rewritten under the shell reading it. Everything a # release ships is a regular file, but a local edit put back from an archive can # be a symlink - somebody pointing a page at persistent storage - and cp without # -d would write the target's content over the path instead, or fail outright # where the target is gone, which after the removals is the worst moment to stop. put_file() { cp -df "$2" "$3.updatewebui.tmp" || return 1 # chmod follows a symlink, so it would land on whatever the link # points at rather than on the link this run is putting back. [ -L "$2" ] || chmod "$1" "$3.updatewebui.tmp" || return 1 mv -f "$3.updatewebui.tmp" "$3" || { rm -f "$3.updatewebui.tmp" return 1 } return 0 } # $1 holds "mode source destination" per line. install_files() { while read -r m s d; do mkdir -p "${d%/*}" || die "cannot create ${d%/*}" put_file "$m" "$s" "$d" 2>"$tmp/put.err" && continue # The merged view of a path can be an entry the kernel is still # serving for an overlay copy that was removed while something # held the file open. Nothing can rename over such an entry - it # answers ESTALE - and it only goes when its last reader closes # and the caches are dropped, or at the next boot. This script # being executed from the very path a --restore removes is how # it happens, and a run that stopped here would stop having # already made its removals. The upper layer underneath is # untouched by any of it, so the file is written straight there: # the flash ends up correct and verify_view is what says the # view of it has not caught up yet. { [ -n "$upper" ] && mkdir -p "$upper${d%/*}" && put_file "$m" "$s" "$upper$d"; } || { [ -s "$tmp/put.err" ] && sed 's/^/ /' "$tmp/put.err" >&2 die "cannot install $d" } done <"$1" return 0 } apply_writes() { [ -s "$tmp/write" ] || return 0 echo "Write $(count "$tmp/write") file(s)" # This script last, so it stays whole for as long as possible. sort -k3 "$tmp/write" | sed "\\|/$scr_name\$|d" >"$tmp/write.ordered" sed -n "\\|/$scr_name\$|p" "$tmp/write" >>"$tmp/write.ordered" install_files "$tmp/write.ordered" } # Entries are unlinked in the overlay itself, and only ever entries - never the # directories holding them. Removing a directory from the upper layer leaves the # merged parent listing empty until the next reboot, with the files underneath # still readable one by one, so nothing short of a directory listing notices. # An emptied upper directory costs one inode and hides nothing; the one # sanctioned way out for it is offer_dir_cleanup, which pairs the rmdir with # an immediate reboot and only ever runs on the user's explicit yes. remove_entries() { : >"$tmp/failed" cat "$@" | while read -r u; do rm -f "$u" 2>/dev/null [ -e "$u" ] && echo "$u" >>"$tmp/failed" done [ -s "$tmp/failed" ] || return 0 echo "Could not remove:" >&2 sed 's/^/ /' "$tmp/failed" >&2 return 1 } apply_removals() { { [ -s "$tmp/drop" ] || [ -s "$tmp/prune" ]; } || return 0 echo "Drop $(count "$tmp/drop") redundant and $(count "$tmp/prune") stale overlay entry(ies)" remove_entries "$tmp/drop" "$tmp/prune" } # The manifest is what the next run compares against to tell a local edit apart # from a file the release simply changed, so recording it is part of installing, # not a footnote to it - including on the run that had nothing to install, which # is otherwise the one case that never gets a baseline. write_manifest() { mkdir -p "$STATE_DIR" && cp -f "$tmp/want" "$MANIFEST" || die "the files are installed but $MANIFEST could not be written, so a later run cannot recognise the customizations made here" } # The record of the directories this script created accumulates across runs, # because a directory made by one run is silently reused by the next. Losing # it costs only --restore's offer to clean the empty shells up, so it is a # warning, not a fatal error. write_dirs() { [ -s "$tmp/newdirs" ] || return 0 { cat "$tmp/newdirs" [ -f "$DIRS" ] && cat "$DIRS" } | sort -u >"$tmp/dirs.u" && cp -f "$tmp/dirs.u" "$DIRS" || echo "$scr_name: could not record the created directories in $DIRS" >&2 return 0 } # Record which WebUI this run deployed, so the Dashboard can show it -- there is # otherwise nothing on the camera that says which build is installed, and a # reporter cannot tell whether an install carried a given fix. It is written to # the state dir rather than into /var/www, and the Dashboard reads it in # preference to the image's own www/.version: an install hides that image copy # without replacing it (the fetched source tree carries none), so reading # /var/www/.version after an install would show the firmware's version, not # what was just deployed. Best-effort throughout -- a failure here never fails # an install that has otherwise succeeded. Format mirrors majestic's -v. write_version() { when=$(date '+%Y-%m-%d %H:%M') if [ -n "$zip_url_given" ]; then # A --url source can carry credentials or a signed query; persist and # show neither. Drop any user:pass@ and any ?query, leaving the scheme, # host and path -- enough to say where it came from. safe=$(printf '%s' "$source_name" | sed -e 's#://[^/@]*@#://#' -e 's/[?].*$//') stamp="$safe, $when" else # Prefer the commit fetch_tree resolved and installed by -- the tree and # its stamp are then the same revision. Only if that was unavailable (or # this is a handed-over run that copied the staged zip rather than # fetching) ask the API for the branch head, and fall back to the branch # name alone. Never a guessed id. sha=$resolved_sha [ -n "$sha" ] || sha=$(curl -fsSL --connect-timeout 10 "https://api.github.com/repos/$REPO/commits/$branch" 2>/dev/null | grep -m1 '"sha"' | sed -n 's/.*"sha": *"\([0-9a-f]\{40\}\).*/\1/p') if [ -n "$sha" ]; then stamp="$branch+$(echo "$sha" | cut -c1-7), $when"; else stamp="$branch, $when"; fi fi # Atomic: a truncated or interrupted write must not leave a partial file # that still wins the Dashboard's non-empty check over the image's own stamp. mkdir -p "$STATE_DIR" && printf '%s\n' "$stamp" >"$STATE_DIR/webui.version.tmp" && mv -f "$STATE_DIR/webui.version.tmp" "$STATE_DIR/webui.version" || echo "$scr_name: could not record the deployed version in $STATE_DIR/webui.version" >&2 } # Forwarding is switched on by a line the SMS page writes into root's crontab, # and nothing else would ever take it out again. Left behind on a camera whose # SMS files are skipped, it would run a missing script once a minute. # # Only that exact line goes, never anything else that happens to mention the # script. And only once the install has gone through: run earlier, a failed # install would leave the old page in place with its forwarding switched off. # A crontab that cannot be edited is an error rather than a footnote, because # the script the line runs is already gone. drop_sms_cron() { [ "$sms" = 1 ] && return 0 grep -qxF '* * * * * /usr/sbin/sms-forward' /etc/crontabs/root 2>/dev/null || return 0 if sed -i '/^\* \* \* \* \* \/usr\/sbin\/sms-forward$/d' /etc/crontabs/root; then echo "Removed SMS forwarding from root's crontab: this firmware carries no SMS page" return 0 fi echo "$scr_name: could not remove the SMS forwarding line from /etc/crontabs/root;" >&2 echo "$scr_name: it now runs a script this firmware does not carry. Delete it by hand." >&2 return 1 } do_install() { fetch_tree build_plan report_plan [ "$dry_run" = 1 ] && exit 0 if [ ! -s "$tmp/write" ] && [ ! -s "$tmp/drop" ] && [ ! -s "$tmp/prune" ]; then echo echo "Already up to date." write_manifest # Still record the version: an up-to-date tree may carry no stamp yet # (the file matched /rom, or a prior install predates this record), and # the Dashboard should show what is actually installed. write_version rm -f /tmp/webui/sysinfo.txt drop_sms_cron || exit 1 return 0 fi echo backup_local apply_writes apply_removals || die "the overlay did not end up as planned, so the manifest was left alone" settle_view write_manifest write_dirs write_version # The per-boot sysinfo cache was written by the release this one just # replaced, and everything derived at detection time — PTZ backend and # support, most visibly — stays frozen at the old release's answers until # something regenerates it. Drop it; the next request rebuilds it with # the new code. (A camera whose PTZ availability changed in this very # install would otherwise hide or show its pad only after a reboot.) rm -f /tmp/webui/sysinfo.txt echo echo "WebUI updated to $source_name." # What was removed must verify too - after write_manifest, so these # expectations never enter the manifest. A pruned path either reveals # the firmware's own file or stops existing altogether. : >"$tmp/gone" sed "s|^$upper||" "$tmp/prune" | while read -r d; do if [ -n "$lower" ] && [ -f "$lower$d" ]; then md5sum "$lower$d" | sed "s| .*| $d|" >>"$tmp/want" else echo "$d" >>"$tmp/gone" fi done verify_view "$tmp/want" "$tmp/gone" drop_sms_cron || exit 1 } # The one place a directory ever leaves the upper layer. rmdir there muddles # the kernel's merged view of the parent until the next mount, so it happens # only on the user's explicit yes and the camera reboots straight after; # declining, or not being on a terminal, leaves everything as it was. # # What is offered follows the same ownership rule as the file removals: # under /var/www, which belongs to the WebUI alone, every upper directory # left empty - that also covers directories made by the versions of this # script that recorded nothing; elsewhere only the directories $DIRS records # this script creating. Deepest first, so a directory holding nothing but # removable children counts as empty itself. offer_dir_cleanup() { [ -n "$upper" ] || return 0 : >"$tmp/dircand" [ -d "$upper/var/www" ] && find "$upper/var/www" -type d | sed "s|^$upper||" >>"$tmp/dircand" if [ -f "$DIRS" ]; then while read -r d; do # Only clean absolute paths, and never let a corrupt record # steer the rmdir outside the upper layer through a ".." # segment. case $d in /?*) ;; *) continue ;; esac case $d in */.. | */../*) continue ;; esac [ -d "$upper$d" ] && echo "$d" done <"$DIRS" >>"$tmp/dircand" fi # Reverse-sorted, a child always precedes its parent (the parent is a # strict prefix), which is the order both the emptiness check and the # rmdirs need. sort -ru "$tmp/dircand" >"$tmp/dircand.r" && mv "$tmp/dircand.r" "$tmp/dircand" : >"$tmp/rmdirs" while read -r d; do empty=1 for e in "$upper$d"/* "$upper$d"/.[!.]* "$upper$d"/..?*; do { [ -e "$e" ] || [ -L "$e" ]; } || continue grep -qxF "${e#$upper}" "$tmp/rmdirs" && continue empty=0 break done [ "$empty" = 1 ] && echo "$d" >>"$tmp/rmdirs" done <"$tmp/dircand" [ -s "$tmp/rmdirs" ] || return 0 echo echo "$(count "$tmp/rmdirs") empty director(ies) are left behind in the overlay:" sort "$tmp/rmdirs" | sed 's/^/ /' if [ "$dry_run" = 1 ]; then echo "A real run would offer to remove them, a step that ends in a reboot." return 0 fi if [ ! -t 0 ]; then echo "They cost one inode each and hide nothing. Removing them needs an" echo "immediate reboot, so run '$scr_name --restore' from a terminal to" echo "be asked about it." return 0 fi echo "Removing a directory from a mounted overlay muddles the kernel's view" echo "of its parent until the next boot, so the camera reboots right after." printf "Remove them and reboot now? [y/N] " read -r ans case $ans in [yY] | [yY][eE][sS]) ;; *) echo "Left in place; they cost one inode each and hide nothing." echo "Run '$scr_name --restore' again to be asked again." return 0 ;; esac : >"$tmp/rmfail" while read -r d; do rmdir "$upper$d" 2>/dev/null || echo "$d" >>"$tmp/rmfail" done <"$tmp/rmdirs" if [ -s "$tmp/rmfail" ]; then grep -vxF -f "$tmp/rmfail" "$tmp/rmdirs" >"$tmp/removed" echo "Could not remove (not empty after all):" sed 's/^/ /' "$tmp/rmfail" else cp "$tmp/rmdirs" "$tmp/removed" fi # What actually went also leaves the record, so a directory that stayed # behind stays remembered. if [ -f "$DIRS" ] && [ -s "$tmp/removed" ]; then grep -vxF -f "$tmp/removed" "$DIRS" >"$tmp/dirs.left" 2>/dev/null if [ -s "$tmp/dirs.left" ]; then cp -f "$tmp/dirs.left" "$DIRS" else rm -f "$DIRS" fi fi sync echo "Rebooting." # The directories are already gone from the upper layer, so a reboot # that did not happen leaves the muddled merged view this whole dance # exists to avoid - that must not read as success. reboot || die "reboot failed; the merged view of the cleaned directories stays confused until the camera is rebooted by hand" exit 0 } # What --restore may take away, and what it must not touch. With a manifest the # answer is exact: a path it records whose overlay copy still holds what the # install put there is this script's to remove, while the same path holding # something else is work done on the camera since, and stays. That is what makes # --restore repeatable - run twice it does the same thing twice, rather than # quietly eating on the second run what the first one handed back. # # Without a manifest there is nothing to consult: the camera was last updated by # a version of this script that recorded nothing, and /var/www belongs to the # WebUI alone, so all of it goes. That is the one path on which a local edit can # still be lost, and one install of this version closes it for good. plan_removals() { : >"$tmp/prune" : >"$tmp/kept" # The manifest existing is the record, empty or not: emptied is what a # previous --restore leaves behind and it means nothing is installed, # while absent means no version that kept a record has ever run here. if [ -f "$MANIFEST" ]; then while read -r h d; do [ -e "$upper$d" ] || continue if [ -f "$upper$d" ] && [ "$(md5sum "$upper$d" | cut -d' ' -f1)" != "$h" ]; then echo "$d" >>"$tmp/kept" continue fi echo "$upper$d" >>"$tmp/prune" done <"$MANIFEST" # Nothing records the whiteouts an older version of this script # wrote, and they still hide the firmware's own files. if [ -d "$upper/var/www" ]; then find "$upper/var/www" ! -type d ! -type f >>"$tmp/prune" # A file in the WebUI's own tree that no install ever put # there is somebody's own doing. It is already safe - only # what the manifest records is removed - but saying so is # the whole reassurance this report exists to give. sed 's/^[0-9a-f]* //' "$MANIFEST" >"$tmp/manifest.paths" find "$upper/var/www" -type f | sed "s|^$upper||" | while read -r d; do grep -qxF "$d" "$tmp/manifest.paths" || echo "$d" >>"$tmp/kept" done fi else [ -d "$upper/var/www" ] && find "$upper/var/www" ! -type d >>"$tmp/prune" # Nothing outside /var/www is recorded for a camera the # manifest-less version of this script updated, and those are # the ones with most to restore, so the fallback is this list of # every path the project has ever installed there. It is a # fallback and nothing more: once a manifest exists it names # what we put outside /var/www, and guessing on top of it is how # a second --restore came to delete an edited /usr/sbin/telegram # that the first had just reported as the user's own. Only where # the firmware has its own copy to reveal: an overlay file with # nothing underneath is somebody's own, not ours to delete. for d in $LEGACY_SHARED; do grep -qxF "$upper$d" "$tmp/prune" && continue { [ -e "$upper$d" ] && [ -n "$lower" ] && [ -f "$lower$d" ]; } && echo "$upper$d" >>"$tmp/prune" done fi return 0 } # Read every archive an install left behind into one staging tree. The names # carry a timestamp, so the glob expands oldest first and a path edited again # between two installs comes back as its latest version. Extraction goes to the # scratch directory rather than over /, and the destination of each member is # recomposed from PAYLOAD afterwards: busybox tar drops a leading / or ../ from # a member name, so nothing can leave the staging tree, and anything that lands # outside the three trees this script owns never leaves it. unpack_archives() { narchives=0 archives_bad=0 : >"$tmp/putback" : >"$tmp/putback.skip" [ "$do_backup" = 1 ] || return 0 set -- "$STATE_DIR"/updatewebui-local-*.tar.gz [ -f "$1" ] || return 0 narchives=$# mkdir -p "$tmp/back" || { archives_bad=$narchives return 0 } for a in "$@"; do rm -rf "$tmp/one" # Each archive is unpacked on its own and merged into the staging # tree only once it has been read whole: tar writes the members # it has already read before it fails on a truncated one, and # straight into the staging tree those half-archives would be put # back by the very run that has just said it could not read them. # busybox tar has no -z either, and a pipeline reports only its # last command, so the decompression is its own checked step. if mkdir -p "$tmp/one" && gzip -dc "$a" >"$tmp/one.tar" 2>/dev/null && tar xf "$tmp/one.tar" -C "$tmp/one" 2>/dev/null && cp -a "$tmp/one/." "$tmp/back/" 2>/dev/null; then continue fi echo "$scr_name: cannot read $a, what it holds is not put back" >&2 archives_bad=$((archives_bad + 1)) done rm -rf "$tmp/one" find "$tmp/back" ! -type d 2>/dev/null | while read -r s; do d=${s#$tmp/back} mine=0 for pair in $PAYLOAD; do case $d in "${pair#*:}"/*) mine=1 ;; esac done [ "$mine" = 1 ] || { echo "$d" >>"$tmp/putback.skip" continue } echo "$(stat -c %a "$s") $s $d" >>"$tmp/putback" done return 0 } # Which of the archived files this run will actually write. Decided before the # removals rather than after them, so that the removals can leave alone whatever # is about to be written again: removing an overlay copy that something holds # open leaves a merged entry no rename can replace, and this script's own file, # customized and then put back, is exactly that case. # # A file the archive holds that the firmware already serves byte for byte is # left alone rather than copied into the overlay: putting it back would hide the # firmware's own file for nothing, which is the very thing --restore is for. The # comparison is against the firmware's copy directly, since the merged path # still holds the installed file at this point. Where the firmware has no copy # to defer to there is nothing to compare against and everything is put back. plan_restore() { : >"$tmp/putback.do" [ -s "$tmp/putback" ] || return 0 while read -r m s d; do # A path still carrying live work on the camera is newer than any # archived copy of it, and is left exactly as it is. grep -qxF "$d" "$tmp/kept" && continue # cmp reads through a symlink, so it would call one equal to a # plain file holding the same bytes and leave the link unmade. if [ -n "$lower" ] && [ ! -L "$s" ] && cmp -s "$s" "$lower$d" 2>/dev/null; then continue fi echo "$m $s $d" >>"$tmp/putback.do" done <"$tmp/putback" return 0 } restore_local() { [ -s "$tmp/putback.do" ] || return 0 echo "Customizations restored ($(count "$tmp/putback.do")):" cut -d' ' -f3 "$tmp/putback.do" | sed 's/^/ /' install_files "$tmp/putback.do" cut -d' ' -f3 "$tmp/putback.do" >"$tmp/restored" # A symlink holds no content of its own, and md5sum would read # whatever it points at, so only regular files carry an expectation. while read -r _ s d; do [ -L "$s" ] && continue md5sum "$s" | sed "s| .*| $d|" done <"$tmp/putback.do" >"$tmp/restored.md5" return 0 } # An archive exists to survive exactly one round trip: an install replaced work # that existed nowhere else, --restore puts it back, and from then on the camera # holds it again and the archive is the redundant copy. So it goes - but only # once every path it held has been read back from the merged view, and never # while an archive that could not even be opened is sitting beside it. drop_archives() { [ "${narchives:-0}" -gt 0 ] || return 0 if [ "$archives_bad" -gt 0 ]; then echo "Keeping the archive(s) in $STATE_DIR: $archives_bad of $narchives could not be read." return 0 fi # Nothing this script wrote holds a path outside the three trees it # installs, so an archive that does was not made here - and deleting it # with "everything they held is on the camera again" would be a lie # about a file that was never put anywhere. if [ -s "$tmp/putback.skip" ]; then echo "Keeping the $narchives archive(s) in $STATE_DIR: $(count "$tmp/putback.skip") path(s) in them lie outside the trees this script installs." return 0 fi # Every path the archives hold, not just the ones written back: one that # was left alone because the firmware already serves that content is on # the camera in the same sense, and until the merged view says so the # archive is still the only copy anybody can read. if [ -s "$tmp/stale" ] && [ -s "$tmp/putback" ]; then cut -d' ' -f3 "$tmp/putback" >"$tmp/putback.paths" if grep -qxF -f "$tmp/putback.paths" "$tmp/stale"; then echo "Keeping the $narchives archive(s) in $STATE_DIR until the camera reads those files back." return 0 fi fi rm -f "$STATE_DIR"/updatewebui-local-*.tar.gz echo "Removed $narchives archive(s) from $STATE_DIR; everything they held is on the camera again." return 0 } do_restore() { [ -n "$upper" ] || die "no overlay is mounted, so there is no installed copy to remove" for f in restored restored.md5 stale want gone; do : >"$tmp/$f"; done plan_removals unpack_archives plan_restore # What is about to be written again is not removed first: the removal # would leave a merged entry the kernel goes on serving for as long as # anything holds the file open, and nothing can rename over one. if [ -s "$tmp/prune" ] && [ -s "$tmp/putback.do" ]; then cut -d' ' -f3 "$tmp/putback.do" | sed "s|^|$upper|" >"$tmp/rewritten" grep -vxF -f "$tmp/rewritten" "$tmp/prune" >"$tmp/prune.left" mv -f "$tmp/prune.left" "$tmp/prune" fi if [ -s "$tmp/prune" ]; then echo "Remove $(count "$tmp/prune") installed file(s), revealing the firmware's WebUI:" sed "s|^$upper| |" "$tmp/prune" elif [ ! -s "$tmp/putback.do" ]; then echo "Nothing is installed over the firmware's WebUI." fi if [ -s "$tmp/kept" ]; then echo echo "Customizations left in place:" sed 's/^/ /' "$tmp/kept" fi if [ -s "$tmp/putback.do" ]; then echo echo "Customizations held in $narchives archive(s), to be put back:" cut -d' ' -f3 "$tmp/putback.do" | sed 's/^/ /' fi if [ "$dry_run" = 1 ]; then # With the files still in place the directories holding them are # not empty yet, so what the offer would find can only be # described. With nothing to remove it is what it would find now. if [ -s "$tmp/prune" ]; then echo echo "The directories this leaves empty would then be offered for" echo "removal, a step that ends in a reboot." elif [ -s "$tmp/putback.do" ]; then return 0 else offer_dir_cleanup fi return 0 fi if [ -s "$tmp/prune" ]; then echo remove_entries "$tmp/prune" || die "the overlay did not end up as planned, so the manifest was left alone" fi # Emptied rather than deleted. Nothing this script installed is left, and # recording that is not the same as recording nothing: an absent manifest # means the camera was last touched by a version that kept no record, and # sends the next --restore down the blanket sweep of /var/www that cannot # tell your files from ours. mkdir -p "$STATE_DIR" && : >"$MANIFEST" || echo "$scr_name: could not empty $MANIFEST" >&2 # The deployed-version record dies with the install: what shows through now # is the firmware's own WebUI, whose version is its www/.version (or none), # not what an install had put here. Drop the per-boot cache too, or the # Dashboard keeps sourcing the removed version until something else # regenerates it. rm -f "$STATE_DIR/webui.version" /tmp/webui/sysinfo.txt settle_view restore_local settle_view # Everything removed must now read back as the firmware's own copy, and a # path the firmware never shipped must be gone from the merged view - # except where an archived edit was put back on top, which is then what # the path is expected to hold. sed "s|^$upper||" "$tmp/prune" | while read -r d; do grep -qxF "$d" "$tmp/restored" && continue if [ -n "$lower" ] && [ -f "$lower$d" ]; then md5sum "$lower$d" | sed "s| .*| $d|" >>"$tmp/want" else echo "$d" >>"$tmp/gone" fi done cat "$tmp/restored.md5" >>"$tmp/want" if [ -s "$tmp/prune" ] || [ -s "$tmp/restored" ]; then echo if [ -s "$tmp/restored" ]; then echo "Restored the WebUI the firmware ships, with $(count "$tmp/restored") customization(s) back on top of it." else echo "Restored the WebUI the firmware ships." fi verify_view "$tmp/want" "$tmp/gone" fi drop_archives offer_dir_cleanup } echo "OpenIPC WebUI installer v$scr_version" case $mode in restore) do_restore ;; *) do_install ;; esac