Skip to content

Commit a4b4c95

Browse files
committed
Merge pull request ottok#3 from openquery/root-and-socket-not-debian-sys-maint
using root with auth_socket plugin instead of debian-sys-maint
2 parents 793bca7 + a863180 commit a4b4c95

File tree

4 files changed

+84
-55
lines changed

4 files changed

+84
-55
lines changed

debian/additions/mariadb.cnf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@
1515
#collation-server = utf8_general_ci
1616
#character_set_server = utf8
1717
#collation_server = utf8_general_ci
18+
19+
# Needed so the root database user can authenticate without a password but
20+
# only when running as the unix root user.
21+
#
22+
# Also available for other users if required.
23+
# See https://mariadb.com/kb/en/unix_socket-authentication-plugin/
24+
plugin-load-add = auth_socket.so

debian/mariadb-server-10.0.README.Debian

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
* MYSQL WON'T START OR STOP?:
22
=============================
3-
You may never ever delete the special mysql user "debian-sys-maint". This
4-
user together with the credentials in /etc/mysql/debian.cnf are used by the
5-
init scripts to stop the server as they would require knowledge of the mysql
6-
root users password else.
7-
So in most of the times you can fix the situation by making sure that the
8-
debian.cnf file contains the right password, e.g. by setting a new one
9-
(remember to do a "flush privileges" then).
3+
You may never ever delete the mysql user "root". Although it has no password
4+
is set, the unix_auth plugin ensure that it can only be run locally as the root
5+
user. The credentials in /etc/mysql/debian.cnf specify the user are used by the
6+
init scripts to stop the server and perform logrotation. So in most of the
7+
time you can fix the situation by making sure that the /etc/mysql/debian.cnf
8+
file specifies the root user and no password.
9+
10+
This used to be the debian-sys-maint user which is no longer used.
1011

1112
* WHAT TO DO AFTER UPGRADES:
1213
============================
@@ -38,23 +39,38 @@ https://mariadb.com/kb
3839

3940
* PASSWORDS:
4041
============
41-
It is strongly recommended to set a password for the mysql root user (which
42-
/usr/bin/mysql -u root -D mysql -e "update user set password=password('new-password') where user='root'"
43-
/usr/bin/mysql -u root -e "flush privileges"
44-
If you already had a password set add "-p" before "-u" to the lines above.
42+
It is strongly recommended you create an admin users for your database
43+
adminstration needs.
44+
45+
If your your local unix account is the one you want to have local super user
46+
access on your database with you can create the following account that will
47+
only work for the local unix user connecting to the database locally.
48+
49+
sudo /usr/bin/mysql -e "GRANT ALL ON *.* TO '$USER'@'localhost' IDENTIFIED VIA unix_socket WITH GRANT OPTION"
50+
51+
To create a local machine account username=USERNAME with a password:
52+
53+
sudo /usr/bin/mysql -e "GRANT ALL ON *.* TO 'USERNAME'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION"
54+
55+
To create a USERNAME user with password 'password' admin user that can access
56+
the DB server over the network:
57+
58+
sudo /usr/bin/mysql -e "GRANT ALL ON *.* TO 'USERNAME'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION"
59+
60+
Scripts should run as a user have have the required grants and be identified via unix_socket.
4561

62+
If you are too tired to type the password in every time and unix_socket auth
63+
doesn't suit your needs, you can store it in the file $HOME/.my.cnf. It should
64+
be chmod 0600 (-rw------- username username .my.cnf) to ensure that nobody else
65+
can read it. Every other configuration parameter can be stored there, too.
4666

47-
If you are tired to type the password in every time or want to automate your
48-
scripts you can store it in the file $HOME/.my.cnf. It should be chmod 0600
49-
(-rw------- username username .my.cnf) to ensure that nobody else can read
50-
it. Every other configuration parameter can be stored there, too. You will
51-
find an example below and more information in the MySQL manual in
52-
/usr/share/doc/mariadb-doc, https://mariadb.com/kb or dev.mysql.com.
67+
For more information in the MariaDB manual in/usr/share/doc/mariadb-doc or
68+
https://mariadb.com/kb/en/configuring-mariadb-with-mycnf/.
5369

54-
ATTENTION: It is necessary, that a .my.cnf from root always contains a "user"
70+
ATTENTION: It is necessary, that a ~/.my.cnf from root always contains a "user"
5571
line wherever there is a "password" line, else, the Debian maintenance
5672
scripts, that use /etc/mysql/debian.cnf, will use the username
57-
"debian-sys-maint" but the password that is in root's .my.cnf. Also note,
73+
"root" but the password that is in root's .my.cnf. Also note,
5874
that every change you make in the /root/.my.cnf will affect the mysql cron
5975
script, too.
6076

debian/mariadb-server-10.0.mysql-server.logrotate

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# If this fails, check debian.conf!
1616
MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
1717
if [ -z "`$MYADMIN ping 2>/dev/null`" ]; then
18-
# Really no mysqld or rather a missing debian-sys-maint user?
18+
# Really no mysqld or in incorrect authentication in /etc/mysql/debian.cnf user?
1919
# If this occurs and is not a error please report a bug.
2020
if ps cax | grep -q mysqld; then
2121
exit 1

debian/mariadb-server-10.0.postinst

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ invoke() {
2121
fi
2222
}
2323

24-
MYSQL_BOOTSTRAP="/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables --default-storage-engine=myisam"
24+
MYSQL_BOOTSTRAP="/usr/sbin/mysqld --bootstrap --user=mysql --skip-grant-tables --default-storage-engine=myisam --plugin-load-add=auth_socket"
2525

2626
test_passwordless_root_login() {
2727
mysql --no-defaults -u root -h localhost </dev/null >/dev/null 2>&1
@@ -40,12 +40,11 @@ set_mysql_rootpw() {
4040
# this avoids us having to call "test" or "[" on $rootpw
4141
cat << EOF > $tfile
4242
USE mysql;
43-
UPDATE user SET password=PASSWORD("$rootpw") WHERE user='root';
43+
SET sql_log_bin=0;
44+
UPDATE user SET password="", plugin="unix_socket" WHERE user='root';
4445
FLUSH PRIVILEGES;
4546
EOF
46-
if grep -q 'PASSWORD("")' $tfile; then
47-
retval=0
48-
elif [ "$1" = "online" ]; then
47+
if [ "$1" = "online" ]; then
4948
mysql --no-defaults -u root -h localhost <$tfile >/dev/null
5049
retval=$?
5150
else
@@ -123,26 +122,30 @@ case "$1" in
123122
# As the binlog cron scripts to need at least the Super_priv, I do first
124123
# the old query which always succeeds and then the new which may or may not.
125124

126-
# recreate the credentials file if not present or without mysql_upgrade stanza
125+
# recreate the credentials file if not present or with debian-sys-maint
126+
# still there
127127
dc=$mysql_cfgdir/debian.cnf;
128-
if [ -e "$dc" -a -n "`fgrep mysql_upgrade $dc 2>/dev/null`" ]; then
129-
pass="`sed -n 's/^[ ]*password *= *// p' $dc | head -n 1`"
130-
else
131-
pass=`perl -e 'print map{("a".."z","A".."Z",0..9)[int(rand(62))]}(1..16)'`;
128+
if [ ! -e "$dc" -o -n "`fgrep debian-sys-maint $dc 2>/dev/null`" ]; then
132129
if [ ! -d "$mysql_cfgdir" ]; then install -o 0 -g 0 -m 0755 -d $mysql_cfgdir; fi
130+
if [ -e "$dc" ]; then
131+
oldconf=`mktemp --tmpdir $mysql_cfgdir -t debian_old_config.XXXXXX`
132+
cp $dc $oldconf
133+
else
134+
oldconf=''
135+
fi
133136
umask 066
134137
cat /dev/null > $dc
135138
umask 022
136139
echo "# Automatically generated for Debian scripts. DO NOT TOUCH!" >>$dc
137140
echo "[client]" >>$dc
138141
echo "host = localhost" >>$dc
139-
echo "user = debian-sys-maint" >>$dc
140-
echo "password = $pass" >>$dc
142+
echo "user = root" >>$dc
143+
echo "password = " >>$dc
141144
echo "socket = $mysql_rundir/mysqld.sock" >>$dc
142145
echo "[mysql_upgrade]" >>$dc
143146
echo "host = localhost" >>$dc
144-
echo "user = debian-sys-maint" >>$dc
145-
echo "password = $pass" >>$dc
147+
echo "user = root" >>$dc
148+
echo "password = " >>$dc
146149
echo "socket = $mysql_rundir/mysqld.sock" >>$dc
147150
echo "basedir = /usr" >>$dc
148151
fi
@@ -153,42 +156,45 @@ case "$1" in
153156
# update privilege tables
154157
password_column_fix_query=`/bin/echo -e \
155158
"USE mysql;\n" \
159+
"SET sql_log_bin=0;\n" \
156160
"ALTER TABLE user CHANGE Password Password char(41) character set latin1 collate latin1_bin DEFAULT '' NOT NULL;"`
157-
replace_query=`/bin/echo -e \
158-
"USE mysql;\n" \
159-
"SET sql_mode='';\n" \
160-
"REPLACE INTO user SET " \
161-
" host='localhost', user='debian-sys-maint', password=password('$pass'), " \
162-
" Select_priv='Y', Insert_priv='Y', Update_priv='Y', Delete_priv='Y', " \
163-
" Create_priv='Y', Drop_priv='Y', Reload_priv='Y', Shutdown_priv='Y', " \
164-
" Process_priv='Y', File_priv='Y', Grant_priv='Y', References_priv='Y', " \
165-
" Index_priv='Y', Alter_priv='Y', Super_priv='Y', Show_db_priv='Y', "\
166-
" Create_tmp_table_priv='Y', Lock_tables_priv='Y', Execute_priv='Y', "\
167-
" Repl_slave_priv='Y', Repl_client_priv='Y', Create_view_priv='Y', "\
168-
" Show_view_priv='Y', Create_routine_priv='Y', Alter_routine_priv='Y', "\
169-
" Create_user_priv='Y', Event_priv='Y', Trigger_priv='Y',"\
170-
" ssl_cipher='', x509_issuer='', x509_subject='';"`;
161+
162+
replace_query="USE mysql; SET sql_mode='', sql_log_bin=0; DROP USER 'debian-sys-maint'@'localhost';"
163+
171164
# Engines supported by etch should be installed per default. The query sequence is supposed
172165
# to be aborted if the CREATE TABLE fails due to an already existent table in which case the
173166
# admin might already have chosen to remove one or more plugins. Newlines are necessary.
174167
install_plugins=`/bin/echo -e \
175168
"USE mysql;\n" \
169+
"SET sql_log_bin=0;\n" \
176170
"CREATE TABLE IF NOT EXISTS plugin (name char(64) COLLATE utf8_bin NOT NULL DEFAULT '', " \
177171
" dl char(128) COLLATE utf8_bin NOT NULL DEFAULT '', " \
178172
" PRIMARY KEY (name)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='MySQL plugins';" `
179173

180174
# Upgrade password column format before the root password gets set.
181175
echo "$password_column_fix_query" | $MYSQL_BOOTSTRAP 2>&1 | $ERR_LOGGER
182-
183-
db_get mariadb-server/root_password && rootpw="$RET"
184-
if ! set_mysql_rootpw; then
185-
password_error="yes"
186-
fi
187-
176+
# install unix_socket plugin but ignore if already there
188177
set +e
189-
echo "$replace_query" | $MYSQL_BOOTSTRAP 2>&1 | $ERR_LOGGER
190178
echo "$install_plugins" | $MYSQL_BOOTSTRAP 2>&1 | $ERR_LOGGER
191179
set -e
180+
# overly complicated here but ensures a plugin unix_socket is installed and is idpotent if already installed
181+
echo "SET sql_log_bin=0; USE mysql; DELIMITER //; CREATE PROCEDURE debian_plugin_install(IN plugin_name CHAR(50), IN soname CHAR(50)) BEGIN DECLARE CONTINUE HANDLER FOR NOT FOUND EXECUTE
182+
inst_plug; set @plugin_name=plugin_name; set @soname=soname ;set @install_plugin=CONCAT(\"INSTALL PLUGIN \",@plugin_name,\" SONAME '\", @soname, \"'\");PREPARE inst_plug FROM @install_plugin ;
183+
select PLUGIN_NAME INTO @a from information_schema.plugins where PLUGIN_NAME=@plugin_name AND PLUGIN_STATUS='ACTIVE' AND PLUGIN_TYPE='AUTHENTICATION' AND PLUGIN_LIBRARY LIKE concat(@soname,'%'
184+
); DEALLOCATE PREPARE inst_plug; END// CALL debian_plugin_install('unix_socket', 'auth_socket') // DROP PROCEDURE debian_plugin_install//" | $MYSQL_BOOTSTRAP 2>&1 | $ERR_LOGGER
185+
186+
db_get mariadb-server/root_password && rootpw="$RET"
187+
if ! set_mysql_rootpw; then
188+
password_error="yes"
189+
# restore old config file if exists
190+
[ -e $oldconf ] && mv $oldconf $dc
191+
else
192+
[ -e $oldconf ] && rm -f $oldconf
193+
# purge debian-sys-maint user
194+
set +e
195+
echo "$replace_query" | $MYSQL_BOOTSTRAP 2>&1 | $ERR_LOGGER
196+
set -e
197+
fi
192198
;;
193199

194200
abort-upgrade|abort-remove|abort-configure)

0 commit comments

Comments
 (0)