Debian Codegraph @

debian/mariadb-server.postinst

raw
1 #!/bin/bash
2 set -e
3
4 # shellcheck source=/dev/null
5 . /usr/share/debconf/confmodule
6
7 if [ -n "$DEBIAN_SCRIPT_DEBUG" ]
8 then
9 set -v -x
10 DEBIAN_SCRIPT_TRACE=1
11 fi
12
13 ${DEBIAN_SCRIPT_TRACE:+ echo "#42#DEBUG# RUNNING $0 $*" 1>&2}
14
15 export PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin
16
17 # This command can be used as pipe to syslog. With "-s" it also logs to stderr.
18 ERR_LOGGER="logger -p daemon.err -t mariadb-server.postinst -i"
19 # Specify syslog tag name so it is clear the entry came from this postinst script.
20 # This will make an error in a logged command immediately apparent by aborting
21 # the install, rather than failing silently and leaving a broken install.
22 set -o pipefail
23
24 case "$1" in
25 configure)
26 # This is needed because mariadb-install-db removes the pid file in /run
27 # and because changed configuration options should take effect immediately.
28 # In case the server wasn't running at all it should be ok if the stop
29 # script fails. I can't tell at this point because of the cleaned /run.
30 set +e
31 invoke-rc.d mariadb stop
32 set -e
33
34 # An existing /etc/init.d/mysql might be on the system if there was a
35 # previous MySQL or MariaDB installation, since /etc/init.d files are
36 # considered config files and stay around even after the package is removed.
37 #
38 # The install step of this package adds a new /etc/init.d/mariadb file. As
39 # we also want to ensure that there are no old (and potentially outdated)
40 # versions of /etc/init.d/mysql we simply replace it using a copy of the
41 # latest 'mariadb' file. This has also the added benefit that anything that
42 # invokes traditional sysv init with either 'mysql' or 'mariadb' will end up
43 # controlling this newly installed MariaDB, and thus we maintain better
44 # backwards compatibility.
45 #
46 # Note that the 'Provides' line is also updated to avoid 'insserv' exiting
47 # on failure (when it is run by update-rc.d) because of duplicate service
48 # names.
49 if [ -f "/etc/init.d/mysql" ] && [ -f "/etc/init.d/mariadb" ]
50 then
51 # Copy init file and rename the service name and filename on the fly
52 sed 's/Provides: mariadb/Provides: mysql/g' /etc/init.d/mariadb > /etc/init.d/mysql
53 # NOTE: Number of spaces/tabs is important here!
54 # Confirm if the sed worked
55 if ! grep --quiet "Provides: mysql" /etc/init.d/mysql
56 then
57 # If not, then delete the file to avoid failures later on
58 rm -f /etc/init.d/mysql
59 echo "Warning! Failed creating a mysql named copy of mariadb init.d file"
60 fi
61 fi
62
63 mariadb_statedir=/usr/share/mariadb
64 mariadb_datadir=/var/lib/mariadb
65 legacy_mariadb_datadir=/var/lib/mysql
66 mariadb_logdir=/var/log/mysql
67 mariadb_cfgdir=/etc/mysql
68 mariadb_upgradedir=/var/lib/mysql-upgrade
69
70 # If new mariadb data directory does not exist, assume we are continuing
71 # to use the legacy directory. Add a config entry to override datadir's
72 # value back to the original.
73 if [ ! -d "$mariadb_datadir" ]
74 then
75 mariadb_datadir="$legacy_mariadb_datadir"
76
77 mkdir -p /etc/mysql/mariadb.conf.d
78
79 {
80 echo "# Override the data directory config to continue using the legacy directory."
81 echo "[mariadbd]"
82 echo "datadir = $legacy_mariadb_datadir"
83 } > /etc/mysql/mariadb.conf.d/99-legacy-datadir.cnf
84
85 echo "Created /etc/mysql/mariadb.conf.d/99-legacy-datadir.cnf for MariaDB to continue using /var/lib/mysql on upgrade."
86 fi
87
88 # Ensure mysql user/group exists before any ownership fixes or
89 # mariadb-install-db runs. Note that systemd-sysusers operates on
90 # /etc/passwd and /etc/group directly.
91 systemd-sysusers ${DPKG_ROOT:+--root="$DPKG_ROOT"} mariadb.conf
92
93 # If the following symlink exists, it is a preserved copy the old data dir
94 # created by the preinst script during a upgrade that would have otherwise
95 # been replaced by an empty mysql dir. This should restore it.
96 for dir in DATADIR LOGDIR
97 do
98
99 if [ "$dir" = "DATADIR" ]
100 then
101 targetdir=$mariadb_datadir
102 else
103 targetdir=$mariadb_logdir
104 fi
105
106 savelink="$mariadb_upgradedir/$dir.link"
107 if [ -L "$savelink" ]
108 then
109 # If the targetdir was a symlink before we upgraded it is supposed
110 # to be either still be present or not existing anymore now.
111 if [ -L "$targetdir" ]
112 then
113 rm "$savelink"
114 elif [ ! -d "$targetdir" ]
115 then
116 mv "$savelink" "$targetdir"
117 else
118 # this should never even happen, but just in case...
119 mariadb_tmp=$(mktemp -d -t mariadb-symlink-restore-XXXXXX)
120 echo "this is very strange! see $mariadb_tmp/README..." >&2
121 mv "$targetdir" "$mariadb_tmp"
122 cat << EOF > "$mariadb_tmp/README"
123
124 If you're reading this, it's most likely because you had replaced /var/lib/mysql
125 with a symlink, then upgraded to a new version of mysql, and then dpkg
126 removed your symlink (see #182747 and others). The mysql packages noticed
127 that this happened, and as a workaround have restored it. However, because
128 /var/lib/mysql seems to have been re-created in the meantime, and because
129 we don't want to rm -rf something we don't know as much about, we are going
130 to leave this unexpected directory here. If your database looks normal,
131 and this is not a symlink to your database, you should be able to blow
132 this all away.
133
134 EOF
135 fi
136 fi
137 rmdir $mariadb_upgradedir 2>/dev/null || true
138
139 done # end 'for dir' loop
140
141 # Upgrading from mysql.com needs might have the root user as auth_socket.
142 # auto.cnf is a sign of a mysql install, that doesn't exist in mariadb.
143 # We use lsof to protect against concurrent access by mysqld (mariadb has
144 # its own projection). We make sure we're not doing this on a MySQL-8.0
145 # directory.
146 # This direct update is needed to enable an authentication mechanism to
147 # perform mariadb-upgrade, (MDEV-22678). To keep the impact minimal, we
148 # skip innodb and set key-buffer-size to 0 as it isn't reused.
149 if [ -f "$mariadb_datadir/auto.cnf" ] &&
150 [ -f "$mariadb_datadir/mysql/user.MYD" ] &&
151 ! lsof -nt "$mariadb_datadir"/mysql/user.MYD > /dev/null &&
152 [ ! -f "$mariadb_datadir/undo_001" ]
153 then
154 echo "UPDATE mysql.user SET plugin='unix_socket' WHERE plugin='auth_socket';" |
155 mariadbd --skip-innodb --key_buffer_size=0 --default-storage-engine=MyISAM --bootstrap 2> /dev/null
156 fi
157
158 # Ensure the existence and right permissions for the database and
159 # log files. Use mkdir option 'Z' to create with correct SELinux context.
160 if [ ! -d "$mariadb_statedir" ] && [ ! -L "$mariadb_statedir" ]
161 then
162 mkdir -Z "$mariadb_statedir"
163 fi
164 if [ ! -d "$mariadb_datadir" ] && [ ! -L "$mariadb_datadir" ]
165 then
166 mkdir -Z "$mariadb_datadir"
167 fi
168 if [ ! -d "$mariadb_logdir" ] && [ ! -L "$mariadb_logdir" ]
169 then
170 mkdir -Z "$mariadb_logdir"
171 fi
172 # When creating an ext3 journal on an already mounted filesystem like e.g.
173 # /var/lib/mysql, you get a .journal file that is not modifiable by chown.
174 # The mariadb_statedir must not be writable by the mysql user under any
175 # circumstances as it contains scripts that are executed by root.
176 set +e
177 find "$mariadb_statedir" \( ! -uid 0 -o ! -gid 0 \) -print0 | xargs -0 -r chown 0:0
178 find "$mariadb_datadir" ! -uid "$(id -u mysql)" -print0 | xargs -0 -r chown mysql
179 find "$mariadb_datadir" -follow -not -group mysql -print0 2>/dev/null \
180 | xargs -0 --no-run-if-empty chgrp mysql
181 chown mysql:adm "$mariadb_logdir"
182 find "$mariadb_logdir" -print0 | xargs -0 -r chown mysql:adm
183 chmod 2750 "$mariadb_logdir"
184 set -e
185
186 ## Set the correct filesystem ownership for the PAM v2 plugin
187 # eg. /usr/lib/x86_64-linux-gnu/mysql/plugin/auth_pam_tool_dir/
188 # NOTE! This is security sensitive, don't allow for a race condition.
189 #
190 # 1. Drop privileges of directory
191 # -> At this point only root can see and execute auth_pam_tool
192 chmod 0700 /usr/lib/mysql/plugin/auth_pam_tool_dir
193 #
194 # 2. Make binary setuid
195 # -> At this point only root can run the setuid binary so no escalation here yet
196 chmod 04755 /usr/lib/mysql/plugin/auth_pam_tool_dir/auth_pam_tool
197 #
198 # 3. Allow user 'mysql' to see and execute auth_pam_tool
199 # -> Now user mysql owns the directory and can see and execute the binary inside
200 # -> Since the binary is setuid, user mysql gets limited root powers here to
201 # run the PAM authentications, which need root (e.g. to validate passwords
202 # against /etc/shadow)
203 chown mysql /usr/lib/mysql/plugin/auth_pam_tool_dir
204
205 # This is important to avoid dataloss when there is a removed
206 # mysql-server version from Woody lying around which used the same
207 # data directory and then somehow gets purged by the admin.
208 db_set mariadb-server/postrm_remove_database false || true
209
210 # Clean up old debian version flag files
211 rm -f $mariadb_datadir/debian-*.flag
212
213 # initiate databases. Output is not allowed by debconf :-(
214 # This will fail if we are upgrading an existing database; in this case
215 # mariadb-upgrade, called from the /etc/mysql/debian-start script, will
216 # handle things.
217 # Debian: beware of the bashisms...
218 # Debian: can safely run on upgrades with existing databases
219 # Workaround for Debian Bug #1022994: failure to create database when
220 # working with libpam-tmpdir (by setting TMPDIR to empty value).
221 set +e
222 TMPDIR='' bash /usr/bin/mariadb-install-db \
223 --datadir="$mariadb_datadir" \
224 --rpm --cross-bootstrap \
225 --user=mysql --disable-log-bin \
226 --skip-test-db 2>&1 |
227 $ERR_LOGGER
228 set -e
229
230 # On new installations root user can connect via unix_socket.
231 # But on upgrades, scripts rely on debian-sys-maint user and
232 # credentials in /etc/mysql/debian.cnf
233 # All tools use --defaults-file=/etc/mysql/debian.cnf
234 # And while it's not needed for new installations, we keep using
235 # --defaults-file option for tools (for the sake of upgrades)
236 # and thus need /etc/mysql/debian.cnf to exist, even if it's empty.
237 # In the long run the goal is to obsolete this file.
238 dc="$mariadb_cfgdir/debian.cnf"
239 if [ ! -d "$mariadb_cfgdir" ]
240 then
241 install -o 0 -g 0 -m 0755 -d $mariadb_cfgdir
242 fi
243
244 if [ ! -e "$dc" ]
245 then
246 cat /dev/null > $dc
247 {
248 echo "# THIS FILE IS OBSOLETE. STOP USING IT IF POSSIBLE.";
249 echo "# This file exists only for backwards compatibility for";
250 echo "# tools that run '--defaults-file=/etc/mysql/debian.cnf'";
251 echo "# and have root level access to the local filesystem.";
252 echo "# With those permissions one can run 'mariadb' directly";
253 echo "# anyway thanks to unix socket authentication and hence";
254 echo "# this file is useless. See package README for more info.";
255 echo "[client]";
256 echo "host = localhost";
257 echo "user = root";
258 echo "[mariadb_upgrade]";
259 echo "host = localhost";
260 echo "user = root";
261 echo "# THIS FILE WILL BE REMOVED IN A FUTURE DEBIAN RELEASE.";
262 } >> $dc
263 fi
264 # Keep it only root-readable, as it always was
265 chown 0:0 $dc
266 chmod 0600 $dc
267
268 # If there is a real AppArmor profile, we reload it.
269 # If the default empty profile is installed, then we remove any old
270 # profile that may be loaded.
271 # This allows upgrade from old versions (that have an apparmor profile
272 # on by default) to work both to disable a default profile, and to keep
273 # any profile installed and maintained by users themselves.
274 profile="/etc/apparmor.d/mariadbd"
275 if [ -f "$profile" ] && aa-status --enabled 2> /dev/null
276 then
277 if grep -q /usr/sbin/mariadbd "$profile" 2> /dev/null
278 then
279 apparmor_parser -r "$profile" || true
280 else
281 echo "/usr/sbin/mariadbd { }" | apparmor_parser --remove 2> /dev/null || true
282 fi
283 fi
284
285 # The introduction of /etc/logrotate.d/mariadb has made the old config
286 # obsolete and it needs to be disabled to prevent logrotate running twice.
287 if [ -f /etc/logrotate.d/mysql-server ]
288 then
289 mv -vf /etc/logrotate.d/mysql-server /etc/logrotate.d/mysql-server.dpkg-bak
290 fi
291
292 # The introduction of versionless server package is not fully backwards
293 # compatible as the purge of an old mariadb-server-x.y package would
294 # commands such as 'deb-systemd-helper purge mariadb.service' and
295 # 'update-rc.d mariadb remove'. Fix it by simply deleting any existing postrm
296 # stanzas opportunistically.
297 for old_postrm_file in /var/lib/dpkg/info/mariadb-server-10.?.postrm
298 do
299 # For loop will always run, but the globbing pattern will be expanded into
300 # something only if there are files that match the pattern, so we need to
301 # not act on it if the result is just the globbing pattern itself.
302 if [ "$old_postrm_file" != "/var/lib/dpkg/info/mariadb-server-10.?.postrm" ]
303 then
304 sed '/Automatically added by dh_installinit/,/End automatically added section/d' \
305 -i "$old_postrm_file" || true
306 sed '/Automatically added by dh_systemd_enable/,/End automatically added section/d' \
307 -i "$old_postrm_file" || true
308 fi
309 done
310 ;;
311
312 abort-upgrade|abort-remove|abort-configure)
313 ;;
314
315 triggered)
316 if [ -d /run/systemd/system ]
317 then
318 systemctl --system daemon-reload
319 elif [ -x /etc/init.d/mariadb ]
320 then
321 invoke-rc.d mariadb restart
322 fi
323 ;;
324
325 *)
326 echo "postinst called with unknown argument '$1'" 1>&2
327 exit 1
328 ;;
329 esac
330
331 db_stop # in case invoke fails
332
333 #DEBHELPER#