summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--install.cgi.sh4
-rw-r--r--[-rwxr-xr-x]lexd-spool.sh35
2 files changed, 35 insertions, 4 deletions
diff --git a/install.cgi.sh b/install.cgi.sh
index bab9d05..a2eb8ed 100644
--- a/install.cgi.sh
+++ b/install.cgi.sh
@@ -49,6 +49,10 @@ mkdir -p "/var/www/var/lex/spool"
chown www:tool "/var/www/var/lex/spool"
chmod 2770 "/var/www/var/lex/spool"
+mkdir -p "/var/www/var/lex/bad"
+chown lex:tool "/var/www/var/lex/bad"
+chmod 770 "/var/www/var/lex/bad"
+
mkdir -p "/var/www/var/lex/dict"
chown lex:tool "/var/www/var/lex/dict"
chmod 770 "/var/www/var/lex/dict"
diff --git a/lexd-spool.sh b/lexd-spool.sh
index c0cf632..9b9b723 100755..100644
--- a/lexd-spool.sh
+++ b/lexd-spool.sh
@@ -9,6 +9,7 @@ SSH_HOST="lex-spool"
TMP_DIR="/var/www/var/lex/tmp"
DICT_DIR="/var/www/var/lex/dict"
SPOOL_DIR="/var/www/var/lex/spool"
+BAD_DIR="/var/www/var/lex/bad"
SOCK_PATH="/var/run/lexd/sock"
SLEEP_INTERVAL=30
@@ -20,8 +21,15 @@ log_info() {
logger -t lexd-spool -p daemon.info "$1"
}
+# Move an unprocessable or corrupt file to the remote bad directory
+quarantine_file() {
+ log_err "Moving unprocessable file to ${SPOOL_FILE}"
+ ssh -F "${SSH_CONFIG}" "${SSH_HOST}" \
+ "mkdir -p '${BAD_DIR}' && mv -f '${SPOOL_DIR}/${SPOOL_FILE}' '${BAD_DIR}/'" 2>/dev/null
+}
+
while true; do
- # Fetch one file
+ # Fetch one file cleanly from spool directory
SPOOL_FILE=$(ssh -F "${SSH_CONFIG}" -n "${SSH_HOST}" \
"ls -1 '${SPOOL_DIR}' 2>/dev/null | head -n 1")
@@ -46,12 +54,23 @@ while true; do
if [ -z "${raw_prompt}" ]; then
log_err "Empty spool file: ${SPOOL_FILE}"
+ quarantine_file
exit 1
fi
- # Log truncated input string (up to 45 characters)
- trunc_prompt=$(print -r -- "${raw_prompt}" | cut -c1-45)
- log_info "Processing: \"${trunc_prompt}\""
+ # Log spool file + prompt snippet, capped at 45 characters total
+ prefix="${SPOOL_FILE}: "
+ prefix_len=${#prefix}
+ max_prompt_len=$(( 45 - prefix_len ))
+
+ if [ ${max_prompt_len} -gt 0 ]; then
+ trunc_prompt=$(print -r -- "${raw_prompt}" | cut -c1-"${max_prompt_len}")
+ log_entry="${prefix}${trunc_prompt}"
+ else
+ log_entry=$(print -r -- "${prefix}" | cut -c1-45)
+ fi
+
+ log_info "Processing: ${log_entry}"
# Pass prompt to socket and insert missing empty lines before uppercase headers
response=$(print -r -- "${raw_prompt}" | nc -w 300 -U "${SOCK_PATH}" | awk '
@@ -68,15 +87,20 @@ while true; do
if [ -z "${response}" ]; then
log_err "Empty socket response for: ${SPOOL_FILE}"
+ quarantine_file
exit 1
fi
+ # Strip leading '<' character if present
+ response="${response#<}"
+
# Extract first word (lowercased)
target_word=$(print -r -- "${response}" \
| awk '{print tolower($1); exit}')
if [ -z "${target_word}" ]; then
log_err "No target filename for: ${SPOOL_FILE}"
+ quarantine_file
exit 1
fi
@@ -84,10 +108,12 @@ while true; do
case "${target_word}" in
*[!a-z-]*)
log_err "Invalid word '${target_word}': ${SPOOL_FILE}"
+ quarantine_file
exit 1
;;
[!a-z]*|*[!a-z])
log_err "Bad bounds '${target_word}': ${SPOOL_FILE}"
+ quarantine_file
exit 1
;;
esac
@@ -108,6 +134,7 @@ while true; do
if [ ${ssh_res} -ne 0 ]; then
log_err "SSH error on ${SPOOL_FILE} (code ${ssh_res}): ${ssh_err}"
+ quarantine_file
exit 1
fi