5 # Merge the client ssh host keys to one file
7 # Maximilian Wilhelm <mwilhelm@math.uni-paderborn.de>
8 # -- Sat, 17 Apr 2004 17:21:09 +0200
11 if [ "${USE_OLD_SSH_KEYSYNC_MERGE}" != "Yes, I will." ]; then
13 This version of ssh-keysync-merge is deprecheated.
15 If you *really* want to use this version, set \$USE_OLD_SSH_KEYSYNC_MERGE to
16 "Yes, I will." and run it again.
18 However, we encourage you to use the newer version...
19 If not allready automagically done, run 'upgrade_sshkeysync' to upgrade
26 # Be verbose by default
29 # Environment (to be checked!)
30 CONFIG_FILE="/etc/rbm/ssh-keysync-server.conf"
31 BASE_DIR="/var/cache/ssh-keysync"
32 KEY_FILES_DIR="${BASE_DIR}/keys"
33 KNOWN_HOSTS="${BASE_DIR}/ssh_known_hosts"
34 KNOWN_HOSTS_OLD="${KNOWN_HOSTS}.old"
41 if [ -f ${CONFIG_FILE} -a -r ${CONFIG_FILE} ]; then
42 if ! source ${CONFIG_FILE}; then
43 echo "Failed to load config file \"${CONFIG_FILE}\", exiting." >&2
47 echo "Unable to load config file \"${CONFIG_FILE}\". File does not exist or is not accessable, exiting." >&2
53 if [ `whoami` != "${VALID_USER}" ]; then
54 echo "Script `basename $0` can only be run as user \"${VALID_USER}\"." >&2
58 # Is there room for us?
59 for dir in "${BASE_DIR}" "${KEY_FILES_DIR}"; do
60 if [ ! -d "${dir}" ]; then
61 echo "The directory ${dir} does not exist, but is neccessary for this script to work!" >&2
62 echo -n "Please create ${dir}" >&2
63 [ "${dir}" == "${BASE_DIR}" ] && echo " and allow user '${VALID_USER}' to write there." >&2
70 if [ -z "${DOMAIN_LIST}" ]; then
71 echo "Error: DOMAIN_LIST not set in $0!" >&2
72 echo "Please edit ${CONFIG_FILE} an set DOMAIN_LIST to the correct value." >&2
78 # Merge all client host keys
81 if [ `ls "${KEY_FILES_DIR}"/*.key 2>/dev/null | wc -l` == 0 ]; then
82 echo "No client host keys available, aborting" >&2
86 [ "${debug}" ] && echo -n "Merging client hosts keys "
89 # create an empty file, if there is no known_hosts file
90 [ ! -f "${KNOWN_HOSTS}" ] && touch "${KNOWN_HOSTS}"
91 # Make backup of old ssh_known_hosts file
92 mv "${KNOWN_HOSTS}" "${KNOWN_HOSTS_OLD}"
93 [ "${debug}" ] && echo -n "."
96 # Go to the working directory
98 [ "${debug}" ] && echo -n ". "
101 echo "# ssh_known_hosts generated by ssh-keysync-merge at "$(date +%d.%m.%Y) > ${KNOWN_HOSTS}
102 echo "# " >> "${KNOWN_HOSTS}"
105 for file in *.key; do
106 convert_file "${file}" >> "${KNOWN_HOSTS}"
107 [ "${debug}" ] && echo -n "."
110 [ "${debug}" ] && echo " done."
116 # convert host key into the right format
118 # convert_file <hostname>.<keytype>
122 # get all needed information
123 HOST=$(echo $1 | cut -d. -f1);
124 IP=`host ${HOST} | awk '{ print $NF }'`
126 expr="s/,/,${HOST}./g"
127 # HOSTNAMES="${HOST},${HOST}."`echo ${DOMAIN_LIST} | tr -d '[:space:]' | sed -e "${expr}"`",${IP}"
128 HOSTNAMES="${HOST},${HOST}.`echo ${DOMAIN_LIST} | sed -e ${expr}`,${IP}"
130 # make sure that ${HOSTNAMES} does not include any white spaces
131 # and appand one white space at the end of ${HOSTNAMES}, to
132 # seperate the following key
133 echo -n ${HOSTNAMES} | tr -d '[:space:]'
137 echo "Usage: convert_file <hostname>.<type>" >&2
141 # Compare present and last version of ssh_known_hosts
144 [ "${debug}" ] && echo -n "Comparing present and last version of knonw_hosts: "
147 TEMPFILE=`tempfile -d /tmp -s skeysync`
150 # one first run, there will not be an old file
151 if [ ! -f "${KNOWN_HOSTS_OLD}" ]; then
152 touch "${KNOWN_HOSTS_OLD}"
155 diff -u "${KNOWN_HOSTS_OLD}" "${KNOWN_HOSTS}" > "${TEMPFILE}"
157 if [ -s "${TEMPFILE}" ]; then
158 # There are differences...
159 if [ `grep -c '^-# ssh_known_hosts\|^+# ssh_known_hosts' "${TEMPFILE}"` == 2 ]; then
160 echo "Attention: Files are different!"
161 echo "==============================="
169 [ "${debug}" ] && echo "equal."
177 # Put ssh_known_hosts file in public web dir
180 [ "${debug}" ] && echo -n "Putting ssh_known_hosts into web directory: "
181 cp "${KNOWN_HOSTS}" "${BASE_DIR}/pub"
182 [ "${debug}" ] && echo " done."
186 # Print a little help message
189 echo "Usage: $0 [ -quiet ] [ -help ]"
190 echo " -quiet Only show warnings"
191 echo " -help Print this help"
196 while [ $# -gt 0 ]; do
198 -quiet) unset debug ;;
218 # if file has changed, send mail