93
93
/* List of hosts to connect to.
95
/* Read options from the given file instead of the default my.cnf
98
/* Read options from the given group.
100
/* File containing client's X509 certificate.
102
/* File containing the private key corresponding to \fItls_cert_file\fR.
104
/* File containing certificates for all of the X509 Certificate
105
/* Authorities the client will recognize. Takes precedence over
108
/* Directory containing X509 Certificate Authority certificates
109
/* in separate individual files.
110
/* .IP tls_verify_cert
111
/* Verify that the server's name matches the common name of the
95
114
/* For example, if you want the map to reference databases of
96
115
/* the name "your_db" and execute a query like this: select
243
272
/* internal function declarations */
244
273
static PLMYSQL *plmysql_init(ARGV *);
245
static MYSQL_RES *plmysql_query(DICT_MYSQL *, const char *, VSTRING *, char *,
274
static MYSQL_RES *plmysql_query(DICT_MYSQL *, const char *, VSTRING *);
247
275
static void plmysql_dealloc(PLMYSQL *);
248
276
static void plmysql_close_host(HOST *);
249
277
static void plmysql_down_host(HOST *);
250
static void plmysql_connect_single(HOST *, char *, char *, char *);
278
static void plmysql_connect_single(DICT_MYSQL *, HOST *);
251
279
static const char *dict_mysql_lookup(DICT *, const char *);
252
280
DICT *dict_mysql_open(const char *, int, int);
253
281
static void dict_mysql_close(DICT *);
351
379
/* do the query - set dict->error & cleanup if there's an error */
352
if ((query_res = plmysql_query(dict_mysql, name, query,
354
dict_mysql->username,
355
dict_mysql->password)) == 0) {
380
if ((query_res = plmysql_query(dict_mysql, name, query)) == 0) {
356
381
dict->error = DICT_ERR_RETRY;
429
454
/* dict_mysql_get_active - get an active connection */
431
static HOST *dict_mysql_get_active(PLMYSQL *PLDB, char *dbname,
432
char *username, char *password)
456
static HOST *dict_mysql_get_active(DICT_MYSQL *dict_mysql)
434
458
const char *myname = "dict_mysql_get_active";
459
PLMYSQL *PLDB = dict_mysql->pldb;
436
461
int count = RETRY_CONN_MAX;
458
483
msg_info("%s: attempting to connect to host %s", myname,
460
plmysql_connect_single(host, dbname, username, password);
485
plmysql_connect_single(dict_mysql, host);
461
486
if (host->stat == STATACTIVE)
486
511
static MYSQL_RES *plmysql_query(DICT_MYSQL *dict_mysql,
487
512
const char *name,
493
PLMYSQL *PLDB = dict_mysql->pldb;
495
516
MYSQL_RES *res = 0;
497
while ((host = dict_mysql_get_active(PLDB, dbname, username, password)) != NULL) {
518
while ((host = dict_mysql_get_active(dict_mysql)) != NULL) {
499
519
#if defined(MYSQL_VERSION_ID) && MYSQL_VERSION_ID >= 40000
534
554
* used to reconnect to a single database when one is down or none is
535
555
* connected yet. Log all errors and set the stat field of host accordingly
537
static void plmysql_connect_single(HOST *host, char *dbname, char *username, char *password)
557
static void plmysql_connect_single(DICT_MYSQL *dict_mysql, HOST *host)
539
559
if ((host->db = mysql_init(NULL)) == NULL)
540
560
msg_fatal("dict_mysql: insufficient memory");
561
if (dict_mysql->option_file)
562
mysql_options(host->db, MYSQL_READ_DEFAULT_FILE, dict_mysql->option_file);
563
if (dict_mysql->option_group)
564
mysql_options(host->db, MYSQL_READ_DEFAULT_GROUP, dict_mysql->option_group);
565
#if defined(MYSQL_VERSION_ID) && MYSQL_VERSION_ID >= 40000
566
if (dict_mysql->tls_key_file || dict_mysql->tls_cert_file ||
567
dict_mysql->tls_CAfile || dict_mysql->tls_CApath || dict_mysql->tls_ciphers)
568
mysql_ssl_set(host->db,
569
dict_mysql->tls_key_file, dict_mysql->tls_cert_file,
570
dict_mysql->tls_CAfile, dict_mysql->tls_CApath,
571
dict_mysql->tls_ciphers);
572
#if MYSQL_VERSION_ID >= 50023
573
if (dict_mysql->tls_verify_cert != -1)
574
mysql_options(host->db, MYSQL_OPT_SSL_VERIFY_SERVER_CERT,
575
&dict_mysql->tls_verify_cert);
541
578
if (mysql_real_connect(host->db,
542
579
(host->type == TYPEINET ? host->name : 0),
580
dict_mysql->username,
581
dict_mysql->password,
547
584
(host->type == TYPEUNIX ? host->name : 0),
583
620
static void mysql_parse_config(DICT_MYSQL *dict_mysql, const char *mysqlcf)
585
const char *myname = "mysqlname_parse";
622
const char *myname = "mysql_parse_config";
586
623
CFG_PARSER *p = dict_mysql->parser;
591
628
dict_mysql->password = cfg_get_str(p, "password", "", 0, 0);
592
629
dict_mysql->dbname = cfg_get_str(p, "dbname", "", 1, 0);
593
630
dict_mysql->result_format = cfg_get_str(p, "result_format", "%s", 1, 0);
631
dict_mysql->option_file = cfg_get_str(p, "option_file", NULL, 0, 0);
632
dict_mysql->option_group = cfg_get_str(p, "option_group", NULL, 0, 0);
633
#if defined(MYSQL_VERSION_ID) && MYSQL_VERSION_ID >= 40000
634
dict_mysql->tls_key_file = cfg_get_str(p, "tls_key_file", NULL, 0, 0);
635
dict_mysql->tls_cert_file = cfg_get_str(p, "tls_cert_file", NULL, 0, 0);
636
dict_mysql->tls_CAfile = cfg_get_str(p, "tls_CAfile", NULL, 0, 0);
637
dict_mysql->tls_CApath = cfg_get_str(p, "tls_CApath", NULL, 0, 0);
638
dict_mysql->tls_ciphers = cfg_get_str(p, "tls_ciphers", NULL, 0, 0);
639
#if MYSQL_VERSION_ID >= 50023
640
dict_mysql->tls_verify_cert = cfg_get_bool(p, "tls_verify_cert", -1);
596
645
* XXX: The default should be non-zero for safety, but that is not
759
808
myfree(dict_mysql->dbname);
760
809
myfree(dict_mysql->query);
761
810
myfree(dict_mysql->result_format);
811
if (dict_mysql->option_file)
812
myfree(dict_mysql->option_file);
813
if (dict_mysql->option_group)
814
myfree(dict_mysql->option_group);
815
#if defined(MYSQL_VERSION_ID) && MYSQL_VERSION_ID >= 40000
816
if (dict_mysql->tls_key_file)
817
myfree(dict_mysql->tls_key_file);
818
if (dict_mysql->tls_cert_file)
819
myfree(dict_mysql->tls_cert_file);
820
if (dict_mysql->tls_CAfile)
821
myfree(dict_mysql->tls_CAfile);
822
if (dict_mysql->tls_CApath)
823
myfree(dict_mysql->tls_CApath);
824
if (dict_mysql->tls_ciphers)
825
myfree(dict_mysql->tls_ciphers);
762
827
if (dict_mysql->hosts)
763
828
argv_free(dict_mysql->hosts);
764
829
if (dict_mysql->ctx)