878a8afd14b315fad9d0536b3608ab64f874a0a8
[debian/amanda] / packaging / common / pre_inst_functions.sh
1 #!/bin/sh
2 # These functions are included by various different installers.
3
4 # We assume that the following variables are defined in the main script:
5 # amanda_user: the amanda account username
6 # amanda_group: the amanda account's group
7 # amandahomedir: a directory to use as amanda's home
8 # deb_uid: sepecific uid amanda_user should get on deb style systems.
9 # dist: used on linux for the distro.
10 # LOGFILE: a log file we append to
11 # os: Linux, Darwin, SunOS, `uname`...
12 # SYSCONFDIR: location of system config files (ie, /etc)
13 # LOGDIR: logging directory for amanda
14
15 # These variables are defined, but null to allow checking names
16 checked_uid=
17 export checked_uid
18 # PreInstall Functions
19
20 create_user() {
21     case $os in
22         Linux|SunOS)
23             # Create the amanda user with a specific uid on deb systems
24             if [ ${dist} = "Debian" -o ${dist} = "Ubuntu" ] ; then
25                 uid_flag="-u ${deb_uid}"
26             else
27                 uid_flag=
28             fi
29             logger "Checking for user: ${amanda_user}"
30             # we want the output of id, but if the user doesn't exist,
31             # sterr output just confuses things
32             ID=`id ${amanda_user} 2> /dev/null | sed 's/uid=\([0-9]*\).*/\1/'`
33             if [ "${ID}x" = "x" ] ; then
34                 logger "Adding ${amanda_user}."
35                 log_output_of useradd -c "Amanda" \
36                             -g ${amanda_group} \
37                             ${uid_flag} \
38                             -d ${AMANDAHOMEDIR} \
39                             -s /bin/bash ${amanda_user}  || \
40                 { logger "WARNING:  Could not create user ${amanda_user}. Installation will fail." ; return 1 ; }
41                 r=`uname -r`
42                 if [ "$os" = "Linux"] && [ ${dist} != "SuSE" ]; then
43                     # Lock the amanda account until admin sets password
44                     log_output_of passwd -l ${amanda_user} || { \
45                         logger "${warning_user_passwd}"; }
46                 fi
47                 if [ "$os" = "SunOS" ]; then
48                     case $r in
49                         5.8|5.9) log_output_of passwd -l ${amanda_user};;
50                         5.10) # Special login-lock, while allowing execution.
51                             log_ouptut_of passwd -N ${amanda_user} || { \
52                                 logger "${warning_user_passwd}"; }
53                         ;;
54                     esac
55                 fi
56
57                 logger "${info_create_user_success}"
58             else
59                 # The user already existed
60                 logger "${info_user_params}"
61             fi
62         ;;
63         Darwin) : #TODO
64         ;;
65     esac
66 }
67
68 add_group() {
69     # Try to add the group, detect via return code if it already exists.
70     # This works on linux and solaris...
71     log_output_of groupadd ${1}
72     # return of 0 means group was added; 9 means group existed.
73     if [ $? = "0" ] || [ $? = "9" ]; then
74         logger "Adding ${amanda_user} to ${1}"
75         case $os in
76             Linux) um_flags="-a -G";;
77             # Solaris does not have -a flag.
78             SunOS) um_flags="-G `groups ${amanda_user}`";;
79         esac
80         # So far, all linux distros have usermod
81         log_output_of usermod -a -G ${1} ${amanda_user} || \
82             { logger "${error_group_member}" ; return 1 ; }
83     else
84         logger "Error: groupadd failed in an unexpected way."
85         return 1
86     fi
87 }
88
89 check_user() {
90     # Check parameters of ${amanda_user}'s account.
91     # $1= user field $2= value to check
92     # group, shell, homedir, UID are valid for $1.
93     # group:  checks the system group file for ${amanda_user}'s
94     #    membership in the group named $2.
95     # shell:  confirms the passwd file's shell field for
96     #    ${amanda_user} is $2
97     # homedir: confirm the passwd file's homedir field for
98     #    ${amanda_user} is $2
99     # UID: confirm that ${amanda_user}'s UID is $2.
100     #
101     # Extra information about the failed check is written to the log.
102     #
103     # Return codes:
104     # 0 = success
105     # 1 = error
106     # 2 = usage error
107     err=0
108     if [ ! $# -eq 2 ]; then
109         echo "check_user(): Wrong number of parameters"
110         return 2
111     fi
112     logger "Verify ${amanda_user}'s $1 = $2 "
113     case $1 in
114         "group")
115             # Check if the group exists, disregarding membership.
116             if `grep "^${2}" ${SYSCONFDIR}/group > /dev/null` ; then
117                 # Assume the user exists, and check the user's primary group.
118                 GROUP=`id ${amanda_user} 2> /dev/null | sed 's/.*gid=[0-9]*(\([^ ()]*\))/\1/'`
119                 if [ ! "x${GROUP}" = "x${2}" ] ; then
120                     logger "${amanda_user} not a member of ${2}"
121                     err=1
122                 fi
123             else
124                 logger "User group '${2}' does not exist"
125                 err=1
126             fi
127         ;;
128         "group-sup*")
129             # Check if a supplementary group exists.
130             SUP_MEM=`awk -F: "\$1 ~ ${2} { print \$4; }" 2> /dev/null`
131             if [ -n "$SUP_MEM" ] ; then
132                 # Check if our user is a member.
133                 if echo "${SUP_MEM}"|grep "${amanda_user}" &> /dev/null ; then
134                     :
135                 else
136                     logger "${amanda_user} is not a member of supplemental group ${2}."
137                     err=1
138                 fi
139             else
140                 logger "Supplemental group ${2} does not exist"
141                 err=1
142             fi
143             ;;
144         "shell")
145             SHELL=`grep "^${amanda_user}" ${SYSCONFDIR}/passwd | cut -d: -f7`
146             wanted_shell=$2; export wanted_shell
147             if [ ! "x${SHELL}" = "x${2}" ] ; then
148                 logger "${warning_user_shell}"
149                 err=1
150             fi
151         ;;
152         "homedir")
153             HOMEDIR=`grep "^${amanda_user}" ${SYSCONFDIR}/passwd | cut -d: -f6`
154             if [ ! "x${HOMEDIR}" = "x${2}" ] ; then
155                 logger "${warning_user_homedir}"
156                 err=1
157             fi
158         ;;
159         "UID")
160             # Debian systems must use a specific UID
161             ID=`id ${amanda_user} 2> /dev/null | sed 's/uid=\([0-9]*\).*/\1/'`
162             if [ ! "${ID}" -eq "${2}" ] ; then
163                 checked_uid=${2}; export checked_uid
164                 logger "${warning_user_uid_debian}"
165                 err=1
166             fi
167         ;;
168         *)
169             echo "check_user(): unknown user parameter."
170             err=2
171         ;;
172     esac
173         
174     return $err
175 }
176
177 check_homedir() {
178         # Checks that the homedir has correct permissions and belongs to correct
179         # user.  Uses $amanda_user and  $amanda_group.
180         if [ -d ${AMANDAHOMEDIR} ] ; then
181             OWNER_GROUP=`ls -dl ${AMANDAHOMEDIR} | awk '{ print $3" "$4; }'`
182             [ "$OWNER_GROUP" = "${amanda_user} ${amanda_group}" ] || \
183                 logger "${warning_homedir_owner}"
184             return $?
185         else
186             logger "homedir ${AMANDAHOMEDIR} does not exist."
187             return 1
188         fi
189 }
190
191 create_homedir() {
192         # Creates the homedir, if necessary, and fixes ownership.
193         if [ ! -d ${AMANDAHOMEDIR} ] ; then
194                 logger "Creating ${AMANDAHOMEDIR}"
195                 log_output_of mkdir -p -m 0750 ${AMANDAHOMEDIR} ||
196                         { logger "WARNING:  Could not create ${AMANDAHOMEDIR}" ; return 1 ; }
197         fi
198         log_output_of chown -R ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR} ||
199                 { logger "WARNING:  Could not chown ${AMANDAHOMEDIR}" ; return 1 ; }
200 }
201
202 create_logdir() {
203     if [ -d ${LOGDIR} ] || [ -f ${LOGDIR} ] ; then
204         logger "Found existing ${LOGDIR}"
205         log_output_of mv ${LOGDIR} ${LOGDIR}.save || \
206             { logger "WARNING:  Could not backup existing log directory: ${LOGDIR}" ; return 1 ; }
207     fi
208     logger "Creating ${LOGDIR}."
209     log_output_of mkdir -p -m 0750 ${LOGDIR} || \
210         { logger "WARNING:  Could not create ${LOGDIR}" ; return 1 ; }
211     if [ -d ${LOGDIR}.save ] || [ -f ${LOGDIR}.save ] ; then
212         # Move the saved log into the logdir.
213         log_output_of mv ${LOGDIR}.save ${LOGDIR}
214     fi
215     log_output_of chown -R ${amanda_user}:${amanda_group} ${LOGDIR} || \
216         { logger "WARNING:  Could not chown ${LOGDIR}" ; return 1 ; }
217 }
218
219 # Info, Warning, and Error strings used by the installer
220
221 info_create_user_success="The '${amanda_user}' user account has been successfully created. 
222  Furthermore, the account has been automatically locked for you for security 
223  purposes.  Once a password for the '${amanda_user}' account has been set, 
224  the user can be unlocked by issuing the following command as root.: 
225
226  # passwd -u ${amanda_user}
227
228  If this is not a new installation of Amanda and you have pre-existing Amanda
229  configurations in ${SYSCONFDIR}/amanda you should ensure that 'dumpuser'
230  is set to '${amanda_user}' in those configurations.  Additionally, you
231  should ensure that ${AMANDAHOMEDIR}/.amandahosts on your client systems
232  is properly configured to allow connections for the user '${amanda_user}'." 
233
234 warning_user_password="!!! WARNING! WARNING! WARNING! WARNING! WARNING! !!!
235 !!!                                                             !!!
236 !!!  The '${amanda_user}' user account for this system has been   !!!
237 !!!  created, however the user has no password set. For         !!!
238 !!!  security purposes this account is normally locked after    !!!
239 !!!  creation.  Unfortunately, when locking this account an     !!!
240 !!!  error occurred.  To ensure the security of your system,    !!!
241 !!!  you should set a password for the user account             !!!
242 !!!  '${amanda_user}' immediately!  To set such a password,     !!!
243 !!!  please issue the following command:                        !!!
244 !!!                                                             !!!
245 !!!   # passwd ${amanda_user}                                   !!!
246 !!!                                                             !!!
247 !!! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!       !!!"
248
249 info_user_params="The Amanda backup software is configured to operate as the user 
250  '${amanda_user}'.  This user exists on your system and has not been modified. 
251  To ensure that Amanda functions properly, please see that the following 
252  parameters are set for that user:
253  SHELL:          /bin/bash
254  HOME:           ${AMANDAHOMEDIR} 
255  Default group:  ${amanda_group}"
256
257 error_group_member="!!!      Nonfatal ERROR.     Nonfatal ERROR.     !!!
258 !!! user '${amanda_user}' is not part of the '${amanda_group}' group,  !!!
259 !!! Amanda will not run until '${amanda_user}' is a member of '${amanda_group}'.  !!!
260 !!!    Nonfatal ERROR.    Nonfatal ERROR.    Nonfatal Error.    !!!"
261
262 warning_user_shell="WARNING: The user '${amanda_user}' has a non-default shell.
263 the default shell is ${wanted_shell}.  Other shells have not been tested."
264
265 warning_user_homedir="!!! WARNING! WARNING! WARNING! WARNING! WARNING! !!!
266 !!! The user '${amanda_user}' must have its home directory set to   !!!
267 !!! '${AMANDAHOMEDIR}' Please correct before using Amanda     !!!
268 !!! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!  !!!"
269
270 warning_user_uid_debian="!!! WARNING! WARNING! WARNING! WARNING! WARNING! !!!
271 !!! Debian packages were built assuming that ${amanda_user} !!!
272 !!! uid = ${checked_uid}.  The uid of ${amanda_user} is different     !!!
273 !!! different on this system.  Files owned by ${checked_uid} must    !!!
274 !!! be chowned to ${amanda_user}.                           !!!
275 !!! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!  !!!"
276
277 warning_homedir_owner="!!! Please ensure that the directory '${AMANDAHOMEDIR}' is owned by !!!
278 !!! the user '${amanda_user}' and group '${amanda_group}'. !!!"
279
280 # --------------- End included Functions -----------------