Control: reassign -1 src:mariadb-10.3
Control: tag -1 + upstream patch

On Thu, Feb 07, 2019 at 03:31:26AM +0100, Alan Krempler wrote:
> Package: python-mysqldb
> Version: 1.3.10-2
> Severity: normal
> 
> When connecting like this:
> connection = MySQLdb.connect(read_default_file=dbconfig)
> lines in the option file specifying a remote host are ignored.
> Whatever host is specified in the option file, python-mysqldb always attempts 
> a
> connection to localhost.
> 
> Named host parameters to MySQLdb.connect() are handled correctly.

Hi,

Thanks for reporting this! I seem to have chased it down to a problem in
MariaDB's default implementation of the mysql_real_connect() function;
it seems that it decides what host to connect to before it checks
whether a configuration file to read was specified.

To the MariaDB maintainers: I'm attaching a simple patch that fixes
the problem for me, as well as a program to test for it. To run the test
program, you'd need a MariaDB user account that is only allowed to
connect from a non-loopback address (I used the 192.168.x.x address on
one of my network cards) and a configuration file with a "client"
section specifying the username and password for the account and
a non-loopback "host" setting.  Without the attached patch, the program
will fail to connect if a hostname is not specified on the command line;
with the patch, a null hostname passed to mysql_real_connect() will
cause the function to read it correctly from the config file.

I believe that this patch, or some version of it, should be submitted
upstream; I could do that if the maintainers are too busy, but I don't
want to step on any toes or miss the chance to use an already
established relationship between the maintainers and the MariaDB
developers.  Also, of course, the maintainers should feel free to
reformat the patch in any way that fits their workflow if they decide to
include it in the Debian package.

Thanks for reading this far, and keep up the great work, everyone!

G'luck,
Peter

-- 
Peter Pentchev  roam@{ringlet.net,debian.org,FreeBSD.org} p...@storpool.com
PGP key:        http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint 2EE7 A7A5 17FC 124C F115  C354 651E EFB0 2527 DF13
Description: Allow the server host to be specified in the defaults file.
Bug-Debian: https://bugs.debian.org/921599
Forwarded: no
Author: Peter Pentchev <r...@ringlet.net>
Last-Update: 2019-02-10

--- a/libmariadb/libmariadb/mariadb_lib.c
+++ b/libmariadb/libmariadb/mariadb_lib.c
@@ -1205,11 +1205,6 @@
   if (!mysql->methods)
     mysql->methods= &MARIADB_DEFAULT_METHODS;
 
-  if (!host || !host[0])
-    host = mysql->options.host;
-
-  ma_set_connect_attrs(mysql, host);
-
   if (net->pvio)  /* check if we are already connected */
   {
     SET_CLIENT_ERROR(mysql, CR_ALREADY_CONNECTED, SQLSTATE_UNKNOWN, 0);
@@ -1228,6 +1223,11 @@
     mysql->options.my_cnf_file=mysql->options.my_cnf_group=0;
   }
 
+  if (!host || !host[0])
+    host = mysql->options.host;
+
+  ma_set_connect_attrs(mysql, host);
+
 #ifndef WIN32
   if (mysql->options.protocol > MYSQL_PROTOCOL_SOCKET)
   {
#include <err.h>
#include <stdio.h>

#include <mysql.h>

__attribute__((noreturn))
static void
errmysql(MYSQL * const conn, const char * const tag)
{
	const int merr = mysql_errno(conn);
	const char * const errstr = mysql_error(conn);
	errx(1, "MySQL error: %s: errno %d: %s", tag, merr, errstr);
}

int main(const int argc, char * const * const argv)
{
	if (argc < 2 || argc > 3)
		errx(1, "Usage: myconn /path/to/fname.cnf [hostname]");
	const char * const conffile = argv[1];
	const char * const host = argc > 2 ? argv[2] : NULL;

	MYSQL * const conn = mysql_init(NULL);
	if (conn == NULL)
		errmysql(conn, "mysql_init");
	printf("Got a MySQL connection structure: %p\n", conn);

	printf("Setting MYSQL_READ_DEFAULT_FILE: %s\n", conffile);
	mysql_options(conn, MYSQL_READ_DEFAULT_FILE, conffile);

	printf("Calling mysql_real_connect() with host %s\n", host);
	if (mysql_real_connect(conn, host, NULL, NULL, NULL, 0,
	    NULL, 0) == NULL)
		errmysql(conn, "mysql_real_connect");

	puts("All done?");
	return 0;
}

Attachment: signature.asc
Description: PGP signature

Reply via email to