mariadb-server-10.5.postinst 8.52 KB
Newer Older
1 2
#!/bin/bash
set -e
3 4 5

. /usr/share/debconf/confmodule

6 7
# Automatically set version to ease maintenance of this file
MAJOR_VER="${DPKG_MAINTSCRIPT_PACKAGE#mariadb-server-}"
8

9 10
if [ -n "$DEBIAN_SCRIPT_DEBUG" ]; then set -v -x; DEBIAN_SCRIPT_TRACE=1; fi
${DEBIAN_SCRIPT_TRACE:+ echo "#42#DEBUG# RUNNING $0 $*" 1>&2 }
11

12 13 14
export PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin

# This command can be used as pipe to syslog. With "-s" it also logs to stderr.
15
ERR_LOGGER="logger -p daemon.err -t mariadb-server-$MAJOR_VER.postinst -i"
16
# Specify syslog tag name so it is clear the entry came from this postinst script.
17 18 19 20 21
# This will make an error in a logged command immediately apparent by aborting
# the install, rather than failing silently and leaving a broken install.
set -o pipefail

invoke() {
22
  systemctl $1 mysql
23 24 25 26
}

case "$1" in
  configure)
27 28 29 30 31 32
    # This is needed because mysql_install_db removes the pid file in /var/run
    # and because changed configuration options should take effect immediately.
    # In case the server wasn't running at all it should be ok if the stop
    # script fails. I can't tell at this point because of the cleaned /var/run.
    set +e; invoke stop; set -e

33 34 35
    mysql_statedir=/usr/share/mysql
    mysql_datadir=/var/lib/mysql
    mysql_logdir=/var/log/mysql
36 37 38
    mysql_cfgdir=/etc/mysql
    mysql_upgradedir=/var/lib/mysql-upgrade

39 40 41
    # If the following symlink exists, it is a preserved copy the old data dir
    # created by the preinst script during a upgrade that would have otherwise
    # been replaced by an empty mysql dir.  This should restore it.
42
    for dir in DATADIR LOGDIR; do
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

      if [ "$dir" = "DATADIR" ]; then
        targetdir=$mysql_datadir
      else
        targetdir=$mysql_logdir
      fi

      savelink="$mysql_upgradedir/$dir.link"
      if [ -L "$savelink" ]; then
        # If the targetdir was a symlink before we upgraded it is supposed
        # to be either still be present or not existing anymore now.
        if [ -L "$targetdir" ]; then
          rm "$savelink"
        elif [ ! -d "$targetdir" ]; then
          mv "$savelink" "$targetdir"
        else
          # this should never even happen, but just in case...
60
          mysql_tmp=$(mktemp -d -t mysql-symlink-restore-XXXXXX)
61 62 63 64
          echo "this is very strange!  see $mysql_tmp/README..." >&2
          mv "$targetdir" "$mysql_tmp"
          cat << EOF > "$mysql_tmp/README"

65
If you're reading this, it's most likely because you had replaced /var/lib/mysql
66
with a symlink, then upgraded to a new version of mysql, and then dpkg
67 68
removed your symlink (see #182747 and others). The mysql packages noticed
that this happened, and as a workaround have restored it. However, because
69
/var/lib/mysql seems to have been re-created in the meantime, and because
70 71
we don't want to rm -rf something we don't know as much about, we are going
to leave this unexpected directory here. If your database looks normal,
72 73 74 75 76
and this is not a symlink to your database, you should be able to blow
this all away.

EOF
        fi
77 78 79
      fi
	    rmdir $mysql_upgradedir 2>/dev/null || true

80
    done
81

82
    # Ensure the existence and right permissions for the database and
83 84 85 86
    # log files. Use mkdir option 'Z' to create with correct SELinux context.
    if [ ! -d "$mysql_statedir" ] && [ ! -L "$mysql_statedir" ]; then mkdir -Z "$mysql_statedir"; fi
    if [ ! -d "$mysql_datadir"  ] && [ ! -L "$mysql_datadir" ]; then mkdir -Z "$mysql_datadir" ; fi
    if [ ! -d "$mysql_logdir"   ] && [ ! -L "$mysql_logdir"  ]; then mkdir -Z "$mysql_logdir"  ; fi
87 88
    # When creating an ext3 jounal on an already mounted filesystem like e.g.
    # /var/lib/mysql, you get a .journal file that is not modifyable by chown.
89
    # The mysql_statedir must not be writable by the mysql user under any
90 91
    # circumstances as it contains scripts that are executed by root.
    set +e
92
    chown -R 0:0 $mysql_statedir
93
    find $mysql_datadir ! -uid "$(id -u mysql)" -print0 | xargs -0 -r chown mysql
94 95
    chown -R mysql:adm $mysql_logdir
    chmod 2750 $mysql_logdir
96 97
    set -e

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    ## Set the correct filesystem ownership for the PAM v2 plugin
    # eg. /usr/lib/x86_64-linux-gnu/mysql/plugin/auth_pam_tool_dir/
    # NOTE! This is security sensitive, don't allow for a race condition.
    #
    # 1. Drop privileges of directory
    # -> At this point only root can see and execute auth_pam_tool
    chmod 0700 /usr/lib/mysql/plugin/auth_pam_tool_dir
    #
    # 2. Make binary setuid
    # -> At this point only root can run the setuid binary so no escalation here yet
    chmod 04755 /usr/lib/mysql/plugin/auth_pam_tool_dir/auth_pam_tool
    #
    # 3. Allow user 'mysql' to see and execute auth_pam_tool
    # -> Now user mysql owns the directory and can see and execute the binary inside
    # -> Since the binary is setuid, user mysql gets limited root powers here to
    #    run the PAM authetications, which need root (e.g. to validate passwords
    #    against /etc/shadow)
115 116
    chown mysql /usr/lib/mysql/plugin/auth_pam_tool_dir

117 118 119
    # This is important to avoid dataloss when there is a removed
    # mysql-server version from Woody lying around which used the same
    # data directory and then somewhen gets purged by the admin.
120 121 122 123 124
    db_set mariadb-server/postrm_remove_database false || true

    # Clean up old flags before setting new one
    rm -f $mysql_datadir/debian-*.flag
    # Flag data dir to avoid downgrades
125
    touch "$mysql_datadir/debian-$MAJOR_VER.flag"
126 127 128 129 130 131 132 133

    # initiate databases. Output is not allowed by debconf :-(
    # This will fail if we are upgrading an existing database; in this case
    # mysql_upgrade, called from the /etc/init.d/mysql start script, will
    # handle things.
    # Debian: beware of the bashisms...
    # Debian: can safely run on upgrades with existing databases
    set +e
134
    bash /usr/bin/mysql_install_db --rpm --cross-bootstrap --user=mysql \
135
                                   --disable-log-bin  --skip-test-db 2>&1 | \
136
                                   $ERR_LOGGER
137
    set -e
138 139

    # To avoid downgrades.
140
    touch "$mysql_statedir/debian-$MAJOR_VER.flag"
141

142 143 144 145 146 147 148
    # On new installations root user can connect via unix_socket.
    # But on upgrades, scripts rely on debian-sys-maint user and
    # credentials in /etc/mysql/debian.cnf
    # All tools use --defaults-file=/etc/mysql/debian.cnf
    # And while it's not needed for new installations, we keep using
    # --defaults-file option for tools (for the sake of upgrades)
    # and thus need /etc/mysql/debian.cnf to exist, even if it's empty.
149
    dc=$mysql_cfgdir/debian.cnf;
150 151 152
    if [ ! -d "$mysql_cfgdir" ]; then
      install -o 0 -g 0 -m 0755 -d $mysql_cfgdir
    fi
153
    if [ ! -e "$dc" ]; then
154 155 156
        cat /dev/null > $dc
        echo "# Automatically generated for Debian scripts. DO NOT TOUCH!" >>$dc
    fi
157
    # Keep it only root-readable, as it always was
158 159 160 161 162 163 164 165 166 167 168
    chown 0:0 $dc
    chmod 0600 $dc

    # If there is a real AppArmor profile, we reload it.
    # If the default empty profile is installed, then we remove any old
    # profile that may be loaded.
    # This allows upgrade from old versions (that have an apparmor profile
    # on by default) to work both to disable a default profile, and to keep
    # any profile installed and maintained by users themselves.
    profile="/etc/apparmor.d/usr.sbin.mysqld"
    if [ -f "$profile" ] && aa-status --enabled 2>/dev/null; then
169 170 171 172 173
      if grep -q /usr/sbin/mysqld "$profile" 2>/dev/null ; then
        apparmor_parser -r "$profile" || true
      else
        echo "/usr/sbin/mysqld { }" | apparmor_parser --remove 2>/dev/null || true
      fi
174
    fi
175 176 177 178 179 180 181

    # copy out any mysqld_safe settings
    systemd_conf=/etc/systemd/system/mariadb.service.d/migrated-from-my.cnf-settings.conf
    if [ -x /usr/bin/mariadb-service-convert -a ! -f "${systemd_conf}" ]; then
      mkdir -p /etc/systemd/system/mariadb.service.d
      /usr/bin/mariadb-service-convert > "${systemd_conf}"
    fi
182 183 184 185 186
  ;;

  abort-upgrade|abort-remove|abort-configure)
  ;;

187 188 189 190 191 192 193
  triggered)
    if [ -x "$(command -v systemctl)" ]; then
      systemctl daemon-reload
    fi
    invoke restart
  ;;

194 195 196 197 198 199 200 201
  *)
    echo "postinst called with unknown argument '$1'" 1>&2
    exit 1
  ;;
esac

db_stop # in case invoke failes

202 203 204
# dh_systemd_start doesn't emit anything since we still ship /etc/init.d/mysql.
# Thus MariaDB server is started via init.d script, which in turn redirects to
# systemctl. If we upgrade from MySQL mysql.service may be masked, which also
205
# means init.d script is disabled. Unmask mysql service explicitly.
206 207 208 209
# Check first that the command exists, to avoid emitting any warning messages.
if [ -x "$(command -v deb-systemd-helper)" ]; then
  deb-systemd-helper unmask mysql.service > /dev/null
fi
210

211
#DEBHELPER#