Imported Upstream version 3.3.2
[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 # wanted_shell:  Linux/SunOS/OSX all keep them in different places
13 # SYSCONFDIR: location of system config files (ie, /etc)
14 # LOGDIR: logging directory for amanda
15
16 # These variables are defined, but null to allow checking names
17 checked_uid=
18 export checked_uid
19 # PreInstall Functions
20
21 create_user() {
22     case $os in
23         Linux|SunOS)
24             # Create the amanda user with a specific uid on deb systems
25             if [ "x${dist}" = "xDebian" ] || [ "x${dist}" = "xUbuntu" ] ; then
26                 uid_flag="-u ${deb_uid}"
27             else
28                 uid_flag=
29             fi
30             logger "Checking for user: ${amanda_user}"
31             # we want the output of id, but if the user doesn't exist,
32             # sterr output just confuses things
33             ID=`id ${amanda_user} 2> /dev/null | sed 's/uid=\([0-9]*\).*/\1/'`
34             if [ "${ID}x" = "x" ] ; then
35                 logger "Adding ${amanda_user}."
36                 log_output_of useradd -c "Amanda" \
37                             -g ${amanda_group} \
38                             ${uid_flag} \
39                             -d ${AMANDAHOMEDIR} \
40                             -s ${wanted_shell} ${amanda_user}  || \
41                 { logger "WARNING:  Could not create user ${amanda_user}. Installation will fail." ; return 1 ; }
42                 logger "Created ${amanda_user} with blank password."
43
44                 if [ "x$os" = "xLinux" ] && [ "x${dist}" != "xSuSE" ]; then
45                     # Lock the amanda account until admin sets password
46                     log_output_of passwd -l ${amanda_user} && { \
47                         logger "${info_create_user_success}"
48                         logger "${info_unlock_account}"
49                     } || { \
50                         logger "${warning_user_passwd}"; }
51                 fi
52                 if [ "x$os" = "xSunOS" ]; then
53                     r=`uname -r`
54                     case $r in
55                         5.8|5.9) log_output_of passwd -l ${amanda_user} ;;
56                         5.10) # Special login-lock, while allowing execution.
57                             log_output_of passwd -N ${amanda_user} && { \
58                                 logger "${info_create_user_success_sol}"
59                                 logger "${info_unlock_account}"
60                             } || { \
61                                 logger "${warning_user_passwd}"; }
62                         ;;
63                     esac
64                 fi
65
66             else
67                 # The user already existed
68                 logger "The Amanda user '${amanda_user}' exists on this system."
69             fi
70         ;;
71         Darwin) : #TODO
72         ;;
73     esac
74 }
75
76 add_group() {
77     # First, try to add the group, detect via return code if it
78     # already exists. Then add ${amanda_user} to the group without
79     # checking if account is already a member.  (check_user should
80     # already have been called).
81     #
82     # Works on linux and solaris.  OSX has different tools.
83
84     [ "x${1}" = "x" ] && { logger "Error: first argument was not a group to add." ; return 1 ; }
85     group_to_add=${1}
86     log_output_of groupadd ${group_to_add}
87     rc=$?
88     # return of 0 means group was added; 9 means group existed.
89     if [ $rc -eq 0 ] || [ $rc -eq 9 ]; then
90         logger "Adding ${amanda_user} to ${group_to_add}"
91         # Generate a comma separated list of existing supplemental groups.
92         # Linux prefaces output with '<username> : '.
93         existing_sup_groups=`groups ${amanda_user}|sed 's/.*: //;s/ /,/g'`
94         # usermod append is -A on Suse, all other linux use -a, and
95         # -A means something else entirely on solaris. So we just use
96         # -G, and append a list of the current groups from id.
97         # So far, all linux distros have usermod
98         log_output_of usermod -G ${existing_sup_groups},${group_to_add} ${amanda_user} || { \
99             logger "Nonfatal ERROR: Failed to add ${group_to_add}."
100             logger "${error_group_member}" ; return 1 ; }
101     else
102         logger "Error: groupadd failed in an unexpected way. return code='$rc'"
103         return 1
104     fi
105 }
106
107
108 # All check_user_* functions check various parameters of ${amanda_user}'s
109 # account. Return codes:
110 # 0 = success
111 # 1 = error
112 # 2 = usage or other error.  more info will be logged
113
114 check_user_group() {
115     # checks the system group file for ${amanda_user}'s membership in
116     # the group named $1.
117     err=0
118     [ "x" = "x$1" ] && { logger "check_user_group: no group given"; return 1; }
119     logger "Verify ${amanda_user}'s primary group = $1 "
120     # Check if the group exists, disregarding membership.
121     group_entry=`grep "^${2}" ${SYSCONFDIR}/group 2> /dev/null`
122     if [ ! "x" = "x${group_entry}" ]; then
123         # Assume the user exists, and check the user's primary group.
124         GROUP=`id ${amanda_user} 2> /dev/null |\
125             cut -d " " -f 2 |\
126             sed 's/.*gid=[0-9]*(\([^ ()]*\))/\1/'`
127         if [ ! "x${GROUP}" = "x${1}" ] ; then
128             logger "${amanda_user} not a member of ${1}"
129             err=1
130         fi
131     else
132         logger "User's primary group '${1}' does not exist"
133         err=1
134     fi
135     return $err
136 }
137
138 check_user_supplemental_group() {
139     # Checks for the group ${1}, and adds ${amanda_user} if missing.
140     # Other groups are preserved.
141     err=0
142     [ "x" = "x$1" ] && { logger "check_user_supplemental_group: no supplemental group given"; return 1; }
143     sup_group=${1}
144     logger "Verify ${amanda_user} is a member of ${sup_group}."
145     # First, check if the supplementary group exists.
146     sup_group_entry=`grep "${sup_group}" ${SYSCONFDIR}/group 2>/dev/null`
147     if [ ! "x" = "x${sup_group_entry}" ]; then
148         SUP_MEM=`echo ${sup_group_entry} | cut -d: -f4`
149         # Check if our user is a member.
150         case ${SUP_MEM} in
151             *${amanda_user}*) : ;;
152             *)
153             logger "${amanda_user} is not a member of supplemental group ${sup_group}."
154             err=1
155             ;;
156         esac
157     else
158         logger "Supplemental group ${sup_group} does not exist"
159         err=1
160     fi
161     return $err
162 }
163
164 check_user_shell() {
165     # Confirms the passwd file's shell field for ${amanda_user} is $1
166     [ "x" = "x$1" ] && { logger "check_user_shell: no shell given"; return 1; }
167     wanted_shell=$1; export wanted_shell
168     logger "Verify ${amanda_user}'s shell is ${wanted_shell}."
169     real_shell=`grep "^${amanda_user}" ${SYSCONFDIR}/passwd | cut -d: -f7`
170     export real_shell
171     if [ ! "x${real_shell}" = "x${wanted_shell}" ] ; then
172         logger "WARNING:  ${amanda_user} default shell= ${wanted_shell}"
173         logger "WARNING: ${amanda_user} existing shell: ${real_shell}"
174         logger "${warning_user_shell}"
175         return 1
176     fi
177 }
178
179 check_user_homedir() {
180     # Confirm the passwd file's homedir field for ${amanda_user} is $1
181     [ "x" = "x$1" ] && { logger "check_user_homedir: no homedir given"; return 1; }
182     HOMEDIR=`grep "^${amanda_user}" ${SYSCONFDIR}/passwd | cut -d: -f6`
183     if [ ! "x${HOMEDIR}" = "x${1}" ] ; then
184         logger "${warning_user_homedir}"
185         return 1
186     fi
187 }
188
189 check_user_uid() {
190     # Confirm that ${amanda_user}'s UID is $1.
191     # Debian systems must use a specific UID
192     [ "x" = "x$1" ] && { logger "check_user_uid: no uid given"; return 1; }
193     ID=`id ${amanda_user} 2> /dev/null | sed 's/uid=\([0-9]*\).*/\1/'`
194     if [ ! ${ID} -eq ${1} ] ; then
195         checked_uid=${1}; export checked_uid
196         logger "${warning_user_uid_debian}"
197         return 1
198     fi
199 }
200
201 check_homedir() {
202         # Checks that the homedir has correct permissions and belongs to correct
203         # user.  Uses $amanda_user and  $amanda_group.
204         if [ -d ${AMANDAHOMEDIR} ] ; then
205             OWNER_GROUP=`ls -dl ${AMANDAHOMEDIR} | awk '{ print $3" "$4; }'`
206             [ "x$OWNER_GROUP" = "x${amanda_user} ${amanda_group}" ] || \
207                 logger "${warning_homedir_owner}"
208             return $?
209         else
210             logger "homedir ${AMANDAHOMEDIR} does not exist."
211             return 1
212         fi
213 }
214
215 create_homedir() {
216         # Creates the homedir, if necessary, and fixes ownership.
217         if [ ! -d ${AMANDAHOMEDIR} ] ; then
218                 logger "Creating ${AMANDAHOMEDIR}"
219                 log_output_of mkdir -p -m 0750 ${AMANDAHOMEDIR} ||
220                         { logger "WARNING:  Could not create ${AMANDAHOMEDIR}" ; return 1 ; }
221         fi
222         log_output_of chown -R ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR} ||
223                 { logger "WARNING:  Could not chown ${AMANDAHOMEDIR}" ; return 1 ; }
224 }
225
226 create_logdir() {
227     if [ -d ${LOGDIR} ] || [ -f ${LOGDIR} ] ; then
228         logger "Found existing ${LOGDIR}"
229         log_output_of mv ${LOGDIR} ${LOGDIR}.save || \
230             { logger "WARNING:  Could not backup existing log directory: ${LOGDIR}" ; return 1 ; }
231     fi
232     logger "Creating ${LOGDIR}."
233     log_output_of mkdir -p -m 0750 ${LOGDIR} || \
234         { logger "WARNING:  Could not create ${LOGDIR}" ; return 1 ; }
235     if [ -d ${LOGDIR}.save ] || [ -f ${LOGDIR}.save ] ; then
236         # Move the saved log into the logdir.
237         log_output_of mv ${LOGDIR}.save ${LOGDIR}
238     fi
239     log_output_of chown -R ${amanda_user}:${amanda_group} ${LOGDIR} || \
240         { logger "WARNING:  Could not chown ${LOGDIR}" ; return 1 ; }
241 }
242
243 # Info, Warning, and Error strings used by the installer
244
245 info_create_user_success="NOTE: Set a password and unlock the ${amanda_user} account before
246  running Amanda."
247
248 info_create_user_success_sol="NOTE: Account is set as non-login.  If interactive login
249 is required: a password must be set, and the account activated for login."
250
251 info_unlock_account=" The superuser can unlock the account by running:
252
253  # passwd -u ${amanda_user}
254 "
255
256 info_existing_installs="Pre-existing Amanda installations must confirm:
257   -${SYSCONFDIR}/amanda/* should have 'dumpuser' set to '${amanda_user}'.
258   -${AMANDAHOMEDIR}/.amandahosts on client systems should allow connections by
259    '${amanda_user}'."
260
261 warning_user_password="WARNING:  '${amanda_user}' no password. An error occured when locking the
262  account.  SET A PASSWORD NOW:
263
264  # passwd ${amanda_user}"
265
266 error_group_member="Nonfatal ERROR:  Amanda will not run until '${amanda_user}' is a member the
267  preceeding group.  Install will continue..."
268
269 warning_user_shell="WARNING: The user '${amanda_user}' has a non-default shell. Other shells have not been tested."
270
271 warning_user_homedir="WARNING: The user '${amanda_user}' must have its home directory set to
272 '${AMANDAHOMEDIR}' Please correct before using Amanda."
273
274 warning_user_uid_debian="WARNING: Debian packages were built assuming that ${amanda_user}
275 uid = ${checked_uid}.  The uid of ${amanda_user} is different on this system.  Files
276 owned by ${checked_uid} must be chowned to ${amanda_user}."
277
278 warning_homedir_owner="WARNING: The ${amanda_user}'s home directory,'${AMANDAHOMEDIR}' ownership must be changed to '${amanda_user}:${amanda_group}'. "
279
280 # --------------- End included Functions -----------------