From f7eaad64a7c5f3bc851f146e1f258d34f398a7d7 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Mon, 22 Jul 2019 15:59:59 -0400 Subject: [PATCH] various fixes and mail improvements --- .bashrc | 10 ++--- brc | 13 +++++-- btrbk-run | 52 ++++++++++++++++++++++--- filesystem/usr/local/bin/mailtest-check | 29 ++++++++++++++ install-my-scripts | 5 +-- mail-setup | 18 +++++++-- mount-latest-subvol | 40 ++++++++++--------- primary-setup | 4 +- radicale-setup | 21 +++++++--- switch-mail-host | 4 +- 10 files changed, 146 insertions(+), 50 deletions(-) create mode 100755 filesystem/usr/local/bin/mailtest-check diff --git a/.bashrc b/.bashrc index 097dd98..53e4138 100644 --- a/.bashrc +++ b/.bashrc @@ -38,19 +38,17 @@ # which we could test for, but I can't think of a way to make that # inherently limited to a single ssh command. I choose to set SendEnv # and AcceptEnv ssh config vars to allow the environment variable -# BASH_LOGIN_SHELL to propagate across ssh. +# BASH_LOGIN_SHELL to propagate across ssh. This also requires that we +# wrap ssh in interactive shells, because, once we export the var, it +# will go into scripts and theres no way to automatically set it. -# assume we want ssh commands to source this file if we are sourcing it, -# and we haven't specified otherwise already -[[ ! $BASH_LOGIN_SHELL ]] && export BASH_LOGIN_SHELL=true -#BASH_LOGIN_SHELL=false # temporary override # first conditions show that we are an ssh command without an interactive shell if [[ $SSH_CONNECTION ]] \ && [[ $- == *c* ]] \ && [[ ! $SSH_TTY ]] \ && [[ $- != *i* ]] \ - && [[ ! $BASH_LOGIN_SHELL == true ]]; then + && [[ $BASH_LOGIN_SHELL != true ]]; then return 0 else if [[ -r /etc/profile ]]; then diff --git a/brc b/brc index cdfac01..bf8548f 100644 --- a/brc +++ b/brc @@ -157,7 +157,7 @@ HISTCONTROL=ignoredups # works in addition to HISTCONTROL to do more flexible things # it could also do the same things as HISTCONTROL and thus replace it, # but meh. dunno why, but just " *" does glob expansion, so use [ ] to avoid it. -HISTIGNORE='pass *:[ ]*:lom*:gram*' +HISTIGNORE='pass *:[ ]*:otp *:oathtool *' export BC_LINE_LENGTH=0 @@ -1713,6 +1713,9 @@ srun() { ssh $1 /tmp/${2##*/} "${@:2}" } +ssh() { + BASH_LOGIN_SHELL=true command ssh "$@" +} sss() { # ssh solo ssh -oControlMaster=no -oControlPath=/ "$@" } @@ -1821,11 +1824,11 @@ _dosieve() { # always run this first, edit the test files, then run the following testsieve() { - _dosieve ~/sieve/maintest.sieve INBOX delete + _dosieve ~/sieve/maintest.sieve ${1:-INBOX} delete } runsieve() { c ~/sieve; cp personal{test,}.sieve; cp lists{test,}.sieve - _dosieve ~/sieve/main.sieve -eW INBOX delete + _dosieve ~/sieve/main.sieve -eW ${1:-INBOX} delete } # mail related @@ -2176,6 +2179,10 @@ if [[ $- == *i* ]]; then if [[ -e /nocow/btrfs-stale ]] && ((`command ls -AUq /nocow/btrfs-stale|wc -l`)); then ps_char="! $ps_char" fi + if [[ -e /nocow/mailtest-failure ]]; then + ps_char="@#$@#$ $ps_char" + fi + source /a/bin/bash_unpublished/source-state if [[ ! $SSH_CLIENT && $MAIL_HOST != $HOSTNAME ]]; then ps_char="@ $ps_char" fi diff --git a/btrbk-run b/btrbk-run index f358970..e30c9c3 100644 --- a/btrbk-run +++ b/btrbk-run @@ -16,8 +16,50 @@ # todo: if we cancel in the middle of a btrfs send, then run again immediately, the received subvolume doesn't get a Received UUID: field, and we won't mount it. Need to figure out a solution that will fix this. -set -eE -o pipefail -trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR +err-bash-trace() { + local -i argc_index=0 frame i start=${1:-0} max_indent=8 indent + local source + local extdebug=false + if [[ $(shopt -p extdebug) == *-s* ]]; then + extdebug=true + fi + for ((frame=0; frame < ${#FUNCNAME[@]}-1; frame++)); do + argc=${BASH_ARGC[frame]} + argc_index+=$argc + ((frame < start)) && continue + if (( ${#BASH_SOURCE[@]} > 1 )); then + source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:" + fi + indent=$((frame-start + 1)) + indent=$((indent < max_indent ? indent : max_indent)) + printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}" + if $extdebug; then + for ((i=argc_index-1; i >= argc_index-argc; i--)); do + printf " %s" "${BASH_ARGV[i]}" + done + fi + echo \' + done + return 0 +} +err-catch() { + set -E; shopt -s extdebug + _err-trap() { + err=$? + exec >&2 + set +x + echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: \`$BASH_COMMAND' returned $err" + err-bash-trace 2 + set -e # err trap does not work within an error trap + "${_errcatch_cleanup[@]:-:}" # note :-: is to be compatible with set -u + echo "$0: exiting with code $err" + exit $err + } + trap _err-trap ERR + set -o pipefail +} +err-catch + [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@" @@ -47,8 +89,8 @@ m() { if $verbose; then printf "$pre %s\n" "$*"; fi; "$@"; } die() { printf "$pre %s\n" "$*" >&2; exit 1; } # latest $MAIL_HOST -if [[ -e /b/bash_unpublished/source-semi-priv ]]; then - source /b/bash_unpublished/source-semi-priv +if [[ -e /b/bash_unpublished/source-state ]]; then + source /b/bash_unpublished/source-state fi # note q is owned by root:1000 @@ -134,7 +176,7 @@ if [[ ! -v targets && ! $source ]]; then if ping -q -c1 -w1 iank.vpn.office.fsf.org &>/dev/null; then home=iank.vpn.office.fsf.org else - home=$HOME_DOMAIN + home=b8.nz fi ;;& kw) diff --git a/filesystem/usr/local/bin/mailtest-check b/filesystem/usr/local/bin/mailtest-check new file mode 100755 index 0000000..73f3b33 --- /dev/null +++ b/filesystem/usr/local/bin/mailtest-check @@ -0,0 +1,29 @@ +#!/bin/bash +set -eE -o pipefail +trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR + +cd /m/md/l/testignore/new +shopt -s nullglob + +last_sec=0 +for file in *; do + if [[ $file -nt $latest ]]; then + latest=$file + fi +done + +if [[ $latest ]]; then + last_sec=$(awk '/^Subject: / {print $3}' $latest) +fi + +now=$(date +%s) +limit=$(( now - 60 * 22 )) + +if (( last_sec < limit )); then + echo $HOSTNAME mailtest failure + touch /nocow/mailtest-failure +else + rm -f /nocow/mailtest-failure +fi + +find -type f -mtime +1 -delete diff --git a/install-my-scripts b/install-my-scripts index 6c7aabf..f534720 100755 --- a/install-my-scripts +++ b/install-my-scripts @@ -28,7 +28,6 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR x="$(readlink -f "$BASH_SOURCE")"; cd ${x%/*} -e() { echo "$*"; "$@"; } - # scripts that would interfere with unmounting /a, put them elsewhere -e install /a/bin/log-quiet/log-once switch-mail-host btrbk-run mount-latest-subvol check-subvol-stale /usr/local/bin +echo install /a/bin/log-quiet/log-once switch-mail-host btrbk-run mount-latest-subvol check-subvol-stale /usr/local/bin +install /a/bin/log-quiet/log-once switch-mail-host btrbk-run mount-latest-subvol check-subvol-stale /usr/local/bin diff --git a/mail-setup b/mail-setup index b8440fb..058fb4f 100755 --- a/mail-setup +++ b/mail-setup @@ -322,7 +322,7 @@ debconf-set-selections </etc/exim4/rcpt_local_acl <<'EOF' @@ -419,7 +419,7 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR [[ $EUID == 0 ]] || exec sudo "$BASH_SOURCE" "$@" -f=/a/bin/bash_unpublished/source-semi-priv +f=/a/bin/bash_unpublished/source-state if [[ -e $f ]]; then source $f fi @@ -543,8 +543,7 @@ exim4-config exim4/mailname string mail.iankelling.org # Other destinations for which mail is accepted: # iank.bid is for testing # mail.iankelling.org is for machines i own -exim4-config exim4/dc_other_hostnames string *.iankelling.org;iankelling.org;*iank.bid;iank.bid;*zroe.org;zroe.org;*.b8.nz;b8.nz - +exim4-config exim4/dc_other_hostnames string *.iankelling.org;iankelling.org;*iank.bid;iank.bid;*zroe.org;zroe.org;*.b8.nz;b8.nz;*.fsf.org;fsf.org @@ -973,6 +972,17 @@ fi systemctl restart exim4 systemctl enable exim4 + +if [[ $HOSTNAME == $MAIL_HOST ]]; then + cat >/etc/cron.d/mailtest <<'EOF' +*/10 * * * * iank echo body_test | mail -s "primary_test $(date +%s) $(date +%Y-%m-%dT%H:%M:%S%z)" iank@posteo.de +2/10 * * * * root /usr/local/bin/mailtest-check +EOF + cp /a/bin/distro-setup/filesystem/usr/local/bin/mailtest-check /usr/local/bin +else + rm -f /etc/cron.d/mailtest +fi + # MAIL_HOST also does radicale, and easier to start and stop it here # for when MAIL_HOST changes, so radicale gets the synced files and # does not stop us from remounting /o. diff --git a/mount-latest-subvol b/mount-latest-subvol index ea1d1f8..5cb226c 100644 --- a/mount-latest-subvol +++ b/mount-latest-subvol @@ -35,28 +35,13 @@ EOF } -errcatch() { - set -E; shopt -s extdebug - _err-trap() { - err=$? - exec >&2 - set +x - echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:in \`$BASH_COMMAND' returned $err" - bash-trace 2 - echo "$0: exiting with code $err" - exit $err - } - trap _err-trap ERR - set -o pipefail -} -bash-trace() { - local -i argc_index=0 frame i start=${1:-1} max_indent=8 indent +err-bash-trace() { + local -i argc_index=0 frame i start=${1:-0} max_indent=8 indent local source local extdebug=false if [[ $(shopt -p extdebug) == *-s* ]]; then extdebug=true fi - for ((frame=0; frame < ${#FUNCNAME[@]}-1; frame++)); do argc=${BASH_ARGC[frame]} argc_index+=$argc @@ -64,7 +49,7 @@ bash-trace() { if (( ${#BASH_SOURCE[@]} > 1 )); then source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:" fi - indent=$((frame-start+1)) + indent=$((frame-start + 1)) indent=$((indent < max_indent ? indent : max_indent)) printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}" if $extdebug; then @@ -74,8 +59,25 @@ bash-trace() { fi echo \' done + return 0 +} +err-catch() { + set -E; shopt -s extdebug + _err-trap() { + err=$? + exec >&2 + set +x + echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: \`$BASH_COMMAND' returned $err" + err-bash-trace 2 + set -e # err trap does not work within an error trap + "${_errcatch_cleanup[@]:-:}" # note :-: is to be compatible with set -u + echo "$0: exiting with code $err" + exit $err + } + trap _err-trap ERR + set -o pipefail } -errcatch +err-catch tu() { while read -r line; do diff --git a/primary-setup b/primary-setup index 4ce132c..22ec48e 100755 --- a/primary-setup +++ b/primary-setup @@ -9,8 +9,8 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR if [[ $1 ]]; then new_host=$1 - sed -ri "s/MAIL_HOST=.*/MAIL_HOST=$new_host/" /a/bin/bash_unpublished/source-semi-priv - source /a/bin/bash_unpublished/source-semi-priv + sed -ri "s/MAIL_HOST=.*/MAIL_HOST=$new_host/" /a/bin/bash_unpublished/source-state + source /a/bin/bash_unpublished/source-state fi if [[ $HOSTNAME == $MAIL_HOST ]]; then diff --git a/radicale-setup b/radicale-setup index ba8fde2..3cfaa82 100755 --- a/radicale-setup +++ b/radicale-setup @@ -1,8 +1,16 @@ -#!/bin/bash -l -set -x +#!/bin/bash + [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@" +if [[ -e /b/errhandle/err ]]; then + source /b/errhandle/err +else + set -eE -o pipefail + trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR +fi +source /a/bin/bash_unpublished/source-state +source /a/bin/distro-functions/src/package-manager-abstractions # davdroid setup instructions at the bottom # main docs: @@ -25,7 +33,7 @@ fi d=/etc/systemd/system/radicale.service.d mkdir -p $d -sudo dd of=$d/override.conf <$d/override.conf </dev/null | grep -qFx '[ fi echo "$0: at_home = $at_home" -source /a/bin/bash_unpublished/source-semi-priv +source /a/bin/bash_unpublished/source-state #### begin convert private hostnames to public hostnames #### #if ! $at_home; then # for var in old_host new_host; do -- 2.30.2