~ubuntu-branches/debian/sid/postfix/sid

« back to all changes in this revision

Viewing changes to src/global/dict_mysql.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones, LaMont Jones, localization folks
  • Date: 2014-02-11 07:44:30 UTC
  • mfrom: (1.1.41)
  • Revision ID: package-import@ubuntu.com-20140211074430-91tdwgjriazawdz4
Tags: 2.11.0-1
[LaMont Jones]

* New upstream release: 2.11.0

[localization folks]

* l10n: Updated German translations.  Closes: #734893 (Helge Kreutzmann)

Show diffs side-by-side

added added

removed removed

Lines of Context:
91
91
/*      releases.
92
92
/* .IP hosts
93
93
/*      List of hosts to connect to.
 
94
/* .IP option_file
 
95
/*      Read options from the given file instead of the default my.cnf
 
96
/*      location.
 
97
/* .IP option_group
 
98
/*      Read options from the given group.
 
99
/* .IP tls_cert_file
 
100
/*      File containing client's X509 certificate.
 
101
/* .IP tls_key_file
 
102
/*      File containing the private key corresponding to \fItls_cert_file\fR.
 
103
/* .IP tls_CAfile
 
104
/*      File containing certificates for all of the X509 Certificate
 
105
/*      Authorities the client will recognize.  Takes precedence over
 
106
/*      \fItls_CApath\fR.
 
107
/* .IP tls_CApath
 
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
 
112
/*      certficate.
94
113
/* .PP
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
217
236
    CFG_PARSER *parser;
218
237
    char   *query;
219
238
    char   *result_format;
 
239
    char   *option_file;
 
240
    char   *option_group;
220
241
    void   *ctx;
221
242
    int     expansion_limit;
222
243
    char   *username;
226
247
    PLMYSQL *pldb;
227
248
#if defined(MYSQL_VERSION_ID) && MYSQL_VERSION_ID >= 40000
228
249
    HOST   *active_host;
 
250
    char   *tls_cert_file;
 
251
    char   *tls_key_file;
 
252
    char   *tls_CAfile;
 
253
    char   *tls_CApath;
 
254
    char   *tls_ciphers;
 
255
#if MYSQL_VERSION_ID >= 50023
 
256
    int     tls_verify_cert;
 
257
#endif
229
258
#endif
230
259
} DICT_MYSQL;
231
260
 
242
271
 
243
272
/* internal function declarations */
244
273
static PLMYSQL *plmysql_init(ARGV *);
245
 
static MYSQL_RES *plmysql_query(DICT_MYSQL *, const char *, VSTRING *, char *,
246
 
                                        char *, 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 *);
349
377
        return (0);
350
378
 
351
379
    /* do the query - set dict->error & cleanup if there's an error */
352
 
    if ((query_res = plmysql_query(dict_mysql, name, query,
353
 
                                   dict_mysql->dbname,
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;
357
382
        return (0);
358
383
    }
428
453
 
429
454
/* dict_mysql_get_active - get an active connection */
430
455
 
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)
433
457
{
434
458
    const char *myname = "dict_mysql_get_active";
 
459
    PLMYSQL *PLDB = dict_mysql->pldb;
435
460
    HOST   *host;
436
461
    int     count = RETRY_CONN_MAX;
437
462
 
457
482
        if (msg_verbose)
458
483
            msg_info("%s: attempting to connect to host %s", myname,
459
484
                     host->hostname);
460
 
        plmysql_connect_single(host, dbname, username, password);
 
485
        plmysql_connect_single(dict_mysql, host);
461
486
        if (host->stat == STATACTIVE)
462
487
            return host;
463
488
    }
485
510
 
486
511
static MYSQL_RES *plmysql_query(DICT_MYSQL *dict_mysql,
487
512
                                        const char *name,
488
 
                                        VSTRING *query,
489
 
                                        char *dbname,
490
 
                                        char *username,
491
 
                                        char *password)
 
513
                                        VSTRING *query)
492
514
{
493
 
    PLMYSQL *PLDB = dict_mysql->pldb;
494
515
    HOST   *host;
495
516
    MYSQL_RES *res = 0;
496
517
 
497
 
    while ((host = dict_mysql_get_active(PLDB, dbname, username, password)) != NULL) {
498
 
 
 
518
    while ((host = dict_mysql_get_active(dict_mysql)) != NULL) {
499
519
#if defined(MYSQL_VERSION_ID) && MYSQL_VERSION_ID >= 40000
500
520
 
501
521
        /*
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
536
556
 */
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)
538
558
{
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);
 
576
#endif
 
577
#endif
541
578
    if (mysql_real_connect(host->db,
542
579
                           (host->type == TYPEINET ? host->name : 0),
543
 
                           username,
544
 
                           password,
545
 
                           dbname,
 
580
                           dict_mysql->username,
 
581
                           dict_mysql->password,
 
582
                           dict_mysql->dbname,
546
583
                           host->port,
547
584
                           (host->type == TYPEUNIX ? host->name : 0),
548
585
                           0)) {
582
619
 
583
620
static void mysql_parse_config(DICT_MYSQL *dict_mysql, const char *mysqlcf)
584
621
{
585
 
    const char *myname = "mysqlname_parse";
 
622
    const char *myname = "mysql_parse_config";
586
623
    CFG_PARSER *p = dict_mysql->parser;
587
624
    VSTRING *buf;
588
625
    char   *hosts;
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);
 
641
#endif
 
642
#endif
594
643
 
595
644
    /*
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);
 
826
#endif
762
827
    if (dict_mysql->hosts)
763
828
        argv_free(dict_mysql->hosts);
764
829
    if (dict_mysql->ctx)