0bc3f9f16d1b852e0ff2518b9dfa19b7b96d9326
[ssh-keysync.git] / files / upgrade_sshkeysync
1 #!/bin/sh
2 #
3 # upgrade_sshkeysync
4
5 CONFIGFILE="/etc/rbm/ssh-keysync-server.conf"
6 CONFIG_OLD="${CONFIGFILE}.oldsave"
7
8 function upgrade_config() {
9
10         echo "=========="
11         echo "Upgrading Configuration file ${CONFIGFILE}... "
12
13         if ! grep -q '\[general\]' "${CONFIGFILE}"; then
14         
15                 echo -n " * Moving old file to ${CONFIG_OLD}... "
16                 mv "${CONFIGFILE}" "${CONFIG_OLD}" && echo "done." || echo "Failed!"
17
18                 if [ -f ${CONFIGOLD} -a -r ${CONFIG_OLD} ]; then
19                         if ! source "${CONFIG_OLD}" ; then
20                                 echo "Failed to load config file \"${CONFIG_OLD}\", exiting." >&2
21                                 exit 1
22                         fi
23                 else
24                         echo "Unable to load config file \"${CONFIG_OLD}\". File does not exist or is not accessable, exiting." >&2
25                         exit 1;
26                 fi
27
28                 # Default values if not there
29                 [ "${BASE_DIR}" ]   || BASE_DIR="/var/cache/ssh-keysync"
30                 [ "${VALID_USER}" ] || VALID_USER="skeysync"
31
32                 # Check domain
33                 if [ "${DOMAIN_LIST}" ]; then
34                         export DOMAIN="`echo ${DOMAIN_LIST} | cut -d, -f1`"
35                         ALT_DOMAINS="`echo ${DOMAIN_LIST} | cut -d, -f2-`"
36                 else
37                         echo "Warning: No domain specification found in the old configuration."
38                         echo "You have to specify one manually to let ssh-keysync-merge work."
39                 fi
40
41                 # Write new config
42                 EXPR_USER="s|^user.*|user = ${VALID_USER}|"
43                 EXPR_BASEDIR="s|^base_dir.*|base_dir = ${BASE_DIR}|"
44
45                 echo -n "Writing new config... "
46                 cat << EOF | sed -e "${EXPR_USER}" -e "${EXPR_BASEDIR}" > "${CONFIGFILE}" && echo "done." || echo "Failed!"
47 #
48 # /etc/rbm/ssh-keysync-server.conf
49 #
50
51 ##
52 # General configuration options
53 [general]
54 base_dir="/var/cache/ssh-keysnc"
55 user="skeysync"
56
57 ##
58 # The domains managed by RBM::sshKeySync::Merge
59 #
60 # [dom.ain.name]
61 # alt_domains = alt.rnateiv.dom.ain.name, another.dom.ain.name
62 # separate_outfile = yes (default)
63 # common_outfile = no (default)
64 #
65 # Example:
66 #
67 #  To merge all hostkeys in the domain 'sourceforge.net' into
68 #  a separate known_host file for this domain and into a global
69 #  known_host file and adding \$hostname.sf.net to each entry,
70 #  you could specify the following:
71
72 #  [sourceforge.net]
73 #  alt_domains = sf.net
74 #  separate_outfile  =no
75 #  common_outfile = yes
76 #
77 #
78 EOF
79
80                 # Add domain stanza
81                 if [ "${DOMAIN}" ]; then
82                         echo "[$DOMAIN]" >> "${CONFIGFILE}"
83                         [ "${ALT_DOMAINS}" ] && echo "alt_domains = ${ALT_DOMAINS}" >> "${CONFIGFILE}"
84                         echo "separate_outfile = 1" >> "${CONFIGFILE}"
85                         echo "common_outfile = 1"   >> "${CONFIGFILE}"
86                 fi
87
88         else
89                 echo " Configuration file ${CONFIGFILE} is up to date."
90         fi
91 }
92
93
94 ##
95 # Upgrade the filesystem structure
96 function upgrade_dirs() {
97         # Check for old style config
98         if ! grep -q '\[general\]' "${CONFIGFILE}"; then
99
100                 if [ -f ${CONFIG_FILE} -a -r ${CONFIG_FILE} ]; then
101                         if ! source "${CONFIGFILE}" ; then
102                                 echo "Failed to load config file \"${CONFIGFILE}\", exiting." >&2
103                                 exit 1
104                         fi
105                 else
106                         echo "Unable to load config file \"${CONFIGFILE}\". File does not exist or is not accessable, exiting." >&2
107                         exit 1;
108                 fi
109         else
110                 DOMAIN=`echo $( grep '^\[' "${CONFIGFILE}" | grep -v 'general' ) | sed -e 's/\[\(.\+\)\].*/\1/'`
111                 BASE_DIR=`grep '^base_dir' "${CONFIGFILE}" | sed -e 's|.*= *\(.*\)$|\1|'`
112         fi
113
114         # Check values
115         if [ -z "${BASE_DIR}" ]; then
116                 export BASE_DIR="/var/cache/ssh-keync"
117                 echo "Warning: BASE_DIR is undefined, defaulting to ${BASE_DIR}">&2
118         fi
119
120         [ "${DOMAIN_LIST}" ] && DOMAIN=`echo ${DOMAIN_LIST} | cut -d, -f1`
121         if [ -z "${DOMAIN}" ]; then
122                 echo "Error: No domain specified in configuration, cannot continue automagic update." >&2
123                 exit 1
124         fi
125
126
127         # Go for it
128         echo "=========="
129         echo "Upgrade file system structure:"
130         if [ -d "${BASE_DIR}/keys" ]; then
131                 echo -n " * Creating domain directory: ${BASE_DIR}/${DOMAIN} ... "
132                 mkdir -p "${BASE_DIR}/${DOMAIN}" && echo "done." || echo "Failed!"
133                 
134                 echo -n " * Moving ${BASE_DIR}/keys to ${BASE_DIR}/${DOMAIN}/keys... "
135                 mv "${BASE_DIR}/keys" "${BASE_DIR}/${DOMAIN}" && echo "done." || echo "Failed!"
136         else
137                 echo " No old style key dir found. Nothing to do for me."
138                 exit 0
139         fi
140 }
141
142
143
144 #
145 # Showtime
146 #
147 upgrade_config
148 upgrade_dirs