diff options
| -rw-r--r-- | deploy.cgi.sh | 2 | ||||
| -rwxr-xr-x | sub.sh | 61 |
2 files changed, 41 insertions, 22 deletions
diff --git a/deploy.cgi.sh b/deploy.cgi.sh index 188d9fc..e6d5eeb 100644 --- a/deploy.cgi.sh +++ b/deploy.cgi.sh @@ -46,7 +46,7 @@ chown www:tool "/var/www/var/lex/spool" chmod 710 "/var/www/var/lex/spool" mkdir -p "/var/www/var/lex/spool/tmp" -chown www:www "/var/www/var/lex/spool/tmp" +chown www:tool "/var/www/var/lex/spool/tmp" chmod 750 "/var/www/var/lex/spool/tmp" mkdir -p "/var/www/var/lex/spool/new" @@ -1,22 +1,31 @@ #!/bin/ksh -# OpenBSD ksh daemon to process remote spool files over SSH +# OpenBSD ksh daemon script to process remote spool files over SSH + +# Fall back to the running user's home if $HOME is unset +USER_HOME="${HOME:-$(eval echo "~${USER}")}" +SSH_CONFIG="${USER_HOME}/.ssh/config" SSH_HOST="lex-spool" -SPOOL_DIR="/var/spool/remote_spool" +SPOOL_DIR="/var/www/var/lex/spool/new" DICT_DIR="/var/www/var/lex/dict" -SOCKET_PATH="/var/run/my_program.sock" -SLEEP_INTERVAL=2 +SOCKET_PATH="/var/run/lexd/sock" +SLEEP_INTERVAL=5 log_err() { - logger -t lexd_sub -p daemon.err "$1" + logger -t spool_proc -p daemon.err "$1" +} + +log_info() { + logger -t spool_proc -p daemon.info "$1" } while true; do - # Grab the first spool file name - LS_CMD="ls -1 '${SPOOL_DIR}' 2>/dev/null | head -n 1" - SPOOL_FILE=$(ssh -n "${SSH_HOST}" "${LS_CMD}") + # Fetch one file + SPOOL_FILE=$(ssh -F "${SSH_CONFIG}" -n "${SSH_HOST}" \ + "ls -1 '${SPOOL_DIR}' 2>/dev/null | head -n 1") + if [ $? -ne 0 ]; then - log_err "Failed to reach remote server ${SSH_HOST}" + log_err "Failed connection attempt to ${SSH_HOST}" sleep "${SLEEP_INTERVAL}" continue fi @@ -27,17 +36,27 @@ while true; do continue fi - # Fork to mitigate resource leaks in the main process ( set -e - # Fetch remote file into Unix socket - CAT_CMD="cat '${SPOOL_DIR}/${SPOOL_FILE}'" - response=$(ssh -n "${SSH_HOST}" "${CAT_CMD}" \ - | nc -U "${SOCKET_PATH}") + # Fetch remote spool file content + raw_prompt=$(ssh -F "${SSH_CONFIG}" -n "${SSH_HOST}" \ + "cat '${SPOOL_DIR}/${SPOOL_FILE}'") + + if [ -z "${raw_prompt}" ]; then + log_err "Empty spool file: ${SPOOL_FILE}" + exit 1 + fi + + # Log truncated input string (up to 60 characters) + trunc_prompt=$(print -r -- "${raw_prompt}" | cut -c1-60) + log_info "Processing: \"${trunc_prompt}\"" + + # Pass prompt to socket + response=$(print -r -- "${raw_prompt}" | nc -w 3 -U "${SOCKET_PATH}") if [ -z "${response}" ]; then - log_err "Empty socket response: ${SPOOL_FILE}" + log_err "Empty socket response for: ${SPOOL_FILE}" exit 1 fi @@ -50,7 +69,7 @@ while true; do exit 1 fi - # Validate target word from socket + # Validate target word case "${target_word}" in *[!a-z-]*) log_err "Invalid word '${target_word}': ${SPOOL_FILE}" @@ -64,12 +83,12 @@ while true; do target_file="${DICT_DIR}/${target_word}" - # Write dict file & remove spool file - WRITE_DEL_CMD="cat > '${target_file}' && \ - rm -f '${SPOOL_DIR}/${SPOOL_FILE}'" - + # Write remote dict file & remove spool file print -r -- "${response}" \ - | ssh -n "${SSH_HOST}" "${WRITE_DEL_CMD}" + | ssh -F "${SSH_CONFIG}" "${SSH_HOST}" \ + "cat > '${target_file}' && rm -f '${SPOOL_DIR}/${SPOOL_FILE}'" + + log_info "Finished processing: ${target_word}" ) if [ $? -ne 0 ]; then |
