diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2026-08-21 19:28:10 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2026-08-22 07:33:46 +0800 |
| commit | 0682cf927984628bcae3e6bcbff7022ae5ff0aa6 (patch) | |
| tree | 6a926d6b1daa87453d8f2f371cd491c5dc773f28 /sub.sh | |
| parent | 3fba5649b1f8bd86c689ff8b4c6330c776220284 (diff) | |
| download | lex-0682cf927984628bcae3e6bcbff7022ae5ff0aa6.tar.gz | |
Fix subscriber and permissions.
Diffstat (limited to 'sub.sh')
| -rwxr-xr-x | sub.sh | 61 |
1 files changed, 40 insertions, 21 deletions
@@ -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 |
