mirror of
https://abf.rosa.ru/djam/openssh.git
synced 2025-02-24 10:13:00 +00:00
74 lines
1.9 KiB
Bash
74 lines
1.9 KiB
Bash
#!/bin/sh
|
|
# Author: Mikhail Novosyolov <m.novosyolov@rosalinux.ru>, 2019
|
|
# You can send a pull request (patch) to https://abf.io/import/openssh or to m.novosyolov@rosalinux.ru
|
|
# Send bugreports to http://bugzilla.rosalinux.ru
|
|
# License: GPLv3
|
|
# The purpose of this script is to create an Avahi service for OpenSSH
|
|
# on the fly and set a real sshd port in it, not just the default 22.
|
|
|
|
set -ef
|
|
|
|
load_vars(){
|
|
SSHD_CONFIG="${SSHD_CONFIG:-/etc/ssh/sshd_config}"
|
|
AVAHI_TEMPLATE="${AVAHI_TEMPLATE:-/etc/ssh/openssh_avahi.service}"
|
|
AVAHI_SERVICE="${AVAHI_SERVICE:-/etc/avahi/services/openssh.service}"
|
|
AVAHI_DIRECTORY="$(dirname "$AVAHI_SERVICE")"
|
|
LOCK_FILE="${LOCK_FILE:-/run/openssh-avahi-helper.marker}"
|
|
for i in "$SSHD_CONFIG" "$AVAHI_TEMPLATE"
|
|
do
|
|
if [ ! -r "$i" ]; then
|
|
echo "Config file ${i} cannot be read!"
|
|
return 1
|
|
fi
|
|
if [ ! -d "$AVAHI_DIRECTORY" ]; then
|
|
echo "Avahi is probably not installed, exiting."
|
|
exit 0
|
|
fi
|
|
if [ ! -w "$AVAHI_DIRECTORY" ]; then
|
|
echo "Error: Avahi service directory is not writable!"
|
|
return 1
|
|
fi
|
|
done
|
|
}
|
|
|
|
get_current_sshd_port(){
|
|
CONF_PORT="$(grep ^Port "${SSHD_CONFIG}" | awk '{print $2}' | tail -n 1 | sed 's/[^0-9]//g')"
|
|
if [ -z "$CONF_PORT" ]; then
|
|
CONF_PORT=22
|
|
fi
|
|
}
|
|
|
|
write_new_avahi_config(){
|
|
cat "$AVAHI_TEMPLATE" | sed -e "s,@PORT@,${CONF_PORT},g" > "$AVAHI_SERVICE"
|
|
# avahi-daemon must automatically load this new service
|
|
echo "${AVAHI_SERVICE} is a temporary file created by openssh-avahi-helper" > "$LOCK_FILE"
|
|
}
|
|
|
|
mk_avahi_service(){
|
|
load_vars
|
|
read_args
|
|
get_current_sshd_port
|
|
write_new_avahi_config
|
|
}
|
|
|
|
poststop_cleanup(){
|
|
if [ -f "$LOCK_FILE" ]; then
|
|
rm -fv "$AVAHI_SERVICE"
|
|
rm -fv "$LOCK_FILE"
|
|
fi
|
|
}
|
|
|
|
read_args(){
|
|
while [ -n "$1" ]
|
|
do
|
|
case "$1" in
|
|
-x|--debug ) set -x ;;
|
|
mk_avahi_service ) mk_avahi_service ;;
|
|
poststop_cleanup ) poststop_cleanup ;;
|
|
esac
|
|
shift
|
|
done
|
|
}
|
|
|
|
load_vars
|
|
read_args "$@"
|