Imported Upstream version 3.3.2
[debian/amanda] / packaging / common / post_inst_functions.sh
1 #!/bin/sh
2 # ------------- Begin Post Install Functions -----------------
3 # These functions are included by various different installers.
4
5 # We assume that the following variables are defined in the main script:
6 # amanda_user: the amanda account username
7 # amanda_group: the amanda account's group
8 # AMANDAHOMEDIR: a directory to use as amanda's home
9 # dist: used on linux for the distro.
10 # install_log: a log file we append to
11 # os: Linux, Mac, Solaris, etc...
12 # SYSCONFDIR: location of system config files (ie, /etc)
13 # LOGDIR: logging directory for amanda
14
15 #TODO: gnutar-lists dir for solaris??
16
17 add_service() {
18     # Only needed on Solaris!
19     entry="amanda       10080/tcp    # amanda backup services"
20     # make sure amanda is in /etc/services
21     if [ -z "`grep 'amanda' ${SYSCONFDIR}/services |grep '10080/tcp'`" ] ; then
22         logger "Adding amanda entry to ${SYSCONFDIR}/services."
23         echo "${entry}" >> ${SYSCONFDIR}/services
24     fi
25
26     # make sure kamanda is in /etc/services
27     entry_2="amanda       10081/tcp    famdc    # amanda backup services (kerberos)"
28     if [ -z "`grep 'kamanda' /etc/services |grep '10081/tcp'`" ] ; then
29         logger "Adding kamanda entry to ${SYSCONFDIR}/services."
30         echo "${entry_2}" >> ${SYSCONFDIR}/services
31     fi
32 }
33
34 create_amandates() {
35         logger "Creating ${AMANDATES}."
36         if [ ! -f ${AMANDATES} ] ; then
37                 touch ${AMANDATES} || { logger "WARNING:  Could not create Amandates." ; return 1; }
38         fi
39 }
40
41 check_amandates() {
42         logger "Ensuring correct permissions for '${AMANDATES}'."
43         log_output_of chown ${amanda_user}:${amanda_group} ${AMANDATES} || \
44                 { logger "WARNING:  Could not chown ${AMANDATES}" ; return 1; }
45         log_output_of chmod 0640 ${AMANDATES} || \
46                 { logger "WARNING:  Could not fix perms on ${AMANDATES}" ; return 1; }
47         if [ -x /sbin/restorecon ] ; then
48                 log_output_of /sbin/restorecon ${AMANDATES} || \
49                         { logger "WARNING:  restorecon execution failed." ; return 1; }
50         fi
51 }
52
53 create_gnupg() {
54         # Install .gnupg directory
55         if [ ! -d ${AMANDAHOMEDIR}/.gnupg ] ; then
56                 logger "Creating '${AMANDAHOMEDIR}/.gnupg'"
57                 log_output_of mkdir ${AMANDAHOMEDIR}/.gnupg || \
58                         { logger "WARNING:  Could not create .gnupg dir" ; return 1; }
59         fi
60 }
61
62 check_gnupg() {
63         logger "Ensuring correct permissions for '${AMANDAHOMEDIR}/.gnupg'."
64         log_output_of chown ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR}/.gnupg || \
65                 { logger "WARNING:  Could not chown .gnupg dir." ; return 1; }
66         log_output_of chmod 700 ${AMANDAHOMEDIR}/.gnupg || \
67                 { logger "WARNING:  Could not set permissions on .gnupg dir." ; return 1; }
68 }
69
70 create_amandahosts() {
71         # Install .amandahosts to server
72         if [ ! -f ${AMANDAHOMEDIR}/.amandahosts ] ; then
73                 logger "Creating ${AMANDAHOMEDIR}/.amandahosts"
74         log_output_of touch ${AMANDAHOMEDIR}/.amandahosts || \
75                 { logger "WARNING:  Could not create .amandahosts file." ; return 1; }
76         fi
77 }
78
79 check_amandahosts_entry() {
80         # Entries for client and server differ slightly 
81         # $1 username (root, ${amanda_user})
82         # subsequent parameters are a list of programs to check (amindexd 
83         # amidxtaped, or amdump)
84         logger "Checking '${AMANDAHOMEDIR}/.amandahosts' for '${@}' entries."
85         # Generate our grep expression
86         expr=""
87         for prog in ${@} ; do
88                 expr=${expr}"[[:blank:]]\+${prog}"
89         done
90         for host in localhost localhost.localdomain ; do
91                 logger "Searching .amandahosts for ^${host}${expr}"
92                 if `grep "^${host}${expr}" ${AMANDAHOMEDIR}/.amandahosts >> /dev/null` ; then
93                         continue
94                 else
95                         add_amandahosts_entry ${host} ${@}
96                 fi
97         done
98 }
99
100 add_amandahosts_entry() {
101         # Add entries to amandahosts.
102         # $@ is a fully formatted entry for amandahosts
103         logger "Appending '${@}' to amandahosts"
104         echo "${@}" >>${AMANDAHOMEDIR}/.amandahosts || \
105                 { logger "WARNING:  Could not append to .amandahosts" ; return 1; }
106 }
107
108 check_amandahosts_perms() {
109         logger "Ensuring correct permissions on .amandahosts"
110         log_output_of chown ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR}/.amandahosts || \
111                 { logger "WARNING:  Could not chown .amandahosts." ; return 1; }
112         log_output_of chmod 0600 ${AMANDAHOMEDIR}/.amandahosts || \
113                 { logger "WARNING:  Could not fix permissions on .amandahosts" ; return 1; }
114 }
115
116 create_ssh_key() {
117         # SSH RSA key generation for amdump and amrecover
118         # $1 must be "server" or "client"
119         KEYDIR="${AMANDAHOMEDIR}/.ssh"
120         if [ $1 = "server" ] ; then
121                 KEYFILE="id_rsa_amdump"
122         elif [ $1 = "client" ] ; then
123                 KEYFILE="id_rsa_amrecover"
124         else
125                 logger "Bad parameter to create_ssh_key" ; return 1
126         fi
127         COMMENT="${amanda_user}@$1"
128         if [ ! -d ${KEYDIR} ] ; then
129                 if [ -f ${KEYDIR} ] ; then
130                         logger "Directory '${KEYDIR}' exists as a file.  Renaming to '${KEYDIR}.save'."
131                         log_output_of mv ${KEYDIR} ${KEYDIR}.save || \
132                                 { logger "WARNING:  Could not backup old .ssh directory." ; return 1; }
133                 fi
134                 logger "Creating directory '${KEYDIR}'."
135                 log_output_of mkdir ${KEYDIR} || \
136                         { logger "WARNING:  Could not create .ssh dir." ; return 1; }
137         fi
138         if [ ! -f ${KEYDIR}/${KEYFILE} ] ; then
139                 logger "Creating ssh RSA key in '${KEYDIR}/${KEYFILE}'"
140                 log_output_of ssh-keygen -q -C $COMMENT -t rsa -f ${KEYDIR}/${KEYFILE} -N '' || \
141                         { logger "WARNING:  Error generating ssh key" ; return 1; }
142         fi
143         logger "Setting ownership and permissions for '${KEYDIR}' and '${KEYDIR}/${KEYFILE}*'"
144         log_output_of chown ${amanda_user}:${amanda_group} ${KEYDIR} ${KEYDIR}/${KEYFILE}* || \
145                 { logger "WARNING:  Could not chown one of ${KEYDIR} or ${KEYFILE}"; return 1; }
146         log_output_of chmod 0750 ${KEYDIR} || \
147                 { logger "WARNING:  Could not fix permissions on ${KEYDIR}"; return 1; }
148         log_output_of chmod 0600 ${KEYDIR}/${KEYFILE}* || \
149                 { logger "WARNING:  Could not fix permissions on ${KEYFILE}"; return 1; }
150 }
151
152 create_profile() {
153         # environment variables (~${amanda_user}/.profile)
154         logger "Checking for '${AMANDAHOMEDIR}/.profile'."
155         if [ ! -f ${AMANDAHOMEDIR}/.profile ] ; then
156                 log_output_of touch ${AMANDAHOMEDIR}/.profile || \
157                         { logger "WARNING:  Could not create .profile" ; return 1; }
158         fi
159 }
160
161 check_profile(){
162     logger "Checking for ${SBINDIR} in path statement."
163     if [ -z "`grep PATH.*${SBINDIR} ${AMANDAHOMEDIR}/.profile`" ] ; then
164         echo "PATH=\"\$PATH:${SBINDIR}\"" >>${AMANDAHOMEDIR}/.profile || \
165             { logger "WARNING:  Could not append to .profile" ; return 1; }
166         echo "export PATH" >>${AMANDAHOMEDIR}/.profile
167     fi
168     case $os in
169       SunOS)
170         sun_paths=/opt/csw/bin:/usr/ucb
171         if [ -z "`grep PATH ${AMANDAHOMEDIR}/.profile | grep ${sun_paths}`" ] ; then
172             echo "PATH=\"$PATH:${SBINDIR}:${sun_paths}\"" >>${AMANDAHOMEDIR}/.profile
173         fi
174       ;;
175     esac
176     logger "Setting ownership and permissions for '${AMANDAHOMEDIR}/.profile'"
177     log_output_of chown ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR}/.profile || \
178         { logger "WARNING:  Could not chown .profile." ; return 1; }
179     log_output_of chmod 0640 ${AMANDAHOMEDIR}/.profile || \
180         { logger "WARNING:  Could not fix permissions on .profile" ; return 1; }
181 }
182
183 install_client_conf() {
184     # Install client config
185     if [ "$os" = "SunOS" ] ; then
186         install="install -m 0600 -u ${amanda_user} -g ${amanda_group}"
187     else
188         install="install -m 0600 -o ${amanda_user} -g ${amanda_group}"
189     fi
190     logger "Checking '${SYSCONFDIR}/amanda/amanda-client.conf' file."
191     if [ ! -f ${SYSCONFDIR}/amanda/amanda-client.conf ] ; then
192         logger "Installing amanda-client.conf."
193         log_output_of ${install} ${AMANDAHOMEDIR}/example/amanda-client.conf \
194             ${SYSCONFDIR}/amanda/ || \
195                 { logger "WARNING:  Could not install amanda-client.conf" ; return 1; }
196     else
197         logger "Note: ${SYSCONFDIR}/amanda/amanda-client.conf exists. Please check ${AMANDAHOMEDIR}/example/amanda-client.conf for updates."
198     fi
199 }
200
201 create_ampassphrase() {
202         # install am_passphrase file to server
203         logger "Checking '${AMANDAHOMEDIR}/.am_passphrase' file."
204         if [ ! -f ${AMANDAHOMEDIR}/.am_passphrase ] ; then
205                 logger "Create '${AMANDAHOMEDIR}/.am_passphrase' file."
206                 log_output_of touch ${AMANDAHOMEDIR}/.am_passphrase || \
207                         { logger "WARNING:  Could not create .am_passphrase." ; return 1; }
208                 phrase=`echo $RANDOM | md5sum | awk '{print $1}'` || \
209                         { logger "WARNING:  Error creating pseudo random passphrase." ; return 1; }
210                 echo ${phrase} >>${AMANDAHOMEDIR}/.am_passphrase
211
212                 log_output_of chown ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR}/.am_passphrase || \
213                         { logger "WARNING:  Could not chown .am_passphrase" ; return 1; }
214                 log_output_of chmod 0600 ${AMANDAHOMEDIR}/.am_passphrase || \
215                         { logger "WARNING:  Could not fix permissions on .am_passphrase" ; return 1; }
216         fi
217
218 }
219
220 create_amtmp() {
221         # Check for existence of and permissions on ${AMTMP}
222         logger "Checking for '${AMTMP}' dir."
223         if [ ! -d ${AMTMP} ]; then
224                 logger "Create '${AMTMP}' dir."
225                 log_output_of mkdir ${AMTMP} || \
226                         { logger "WARNING:  Could not create ${AMTMP}." ; return 1; }
227                 log_output_of chown ${amanda_user}:${amanda_group} ${AMTMP} || \
228                         { logger "WARNING:  Could not chown ${AMTMP}" ; return 1; }
229         fi
230 }
231
232 # ------------- End Post Install Functions -----------------