~ubuntu-branches/ubuntu/saucy/postfix/saucy

« back to all changes in this revision

Viewing changes to src/global/dict_sqlite.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2011-02-22 11:20:43 UTC
  • mfrom: (1.1.27 upstream)
  • Revision ID: james.westby@ubuntu.com-20110222112043-c34ht219w3ybrilr
Tags: 2.8.0-2
* a little more lintian cleanup
* Fix missing format strings in smtp-sink.c

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*++
 
2
/* NAME
 
3
/*      dict_sqlite 3
 
4
/* SUMMARY
 
5
/*      dictionary manager interface to SQLite3 databases
 
6
/* SYNOPSIS
 
7
/*      #include <dict_sqlite.h>
 
8
/*
 
9
/*      DICT    *dict_sqlite_open(name, open_flags, dict_flags)
 
10
/*      const char *name;
 
11
/*      int     open_flags;
 
12
/*      int     dict_flags;
 
13
/* DESCRIPTION
 
14
/*      dict_sqlite_open() creates a dictionary of type 'sqlite'.
 
15
/*      This dictionary is an interface for the postfix key->value
 
16
/*      mappings to SQLite.  The result is a pointer to the installed
 
17
/*      dictionary.
 
18
/* .PP
 
19
/*      Arguments:
 
20
/* .IP name
 
21
/*      Either the path to the SQLite configuration file (if it
 
22
/*      starts with '/' or '.'), or the prefix which will be used
 
23
/*      to obtain main.cf configuration parameters for this search.
 
24
/*
 
25
/*      In the first case, the configuration parameters below are
 
26
/*      specified in the file as \fIname\fR=\fIvalue\fR pairs.
 
27
/*
 
28
/*      In the second case, the configuration parameters are prefixed
 
29
/*      with the value of \fIname\fR and an underscore, and they
 
30
/*      are specified in main.cf.  For example, if this value is
 
31
/*      \fIsqlitecon\fR, the parameters would look like
 
32
/*      \fIsqlitecon_dbpath\fR, \fIsqlitecon_query\fR, and so on.
 
33
/* .IP open_flags
 
34
/*      Must be O_RDONLY.
 
35
/* .IP dict_flags
 
36
/*      See dict_open(3).
 
37
/* .PP
 
38
/*      Configuration parameters:
 
39
/* .IP dbpath
 
40
/*      Path to SQLite database
 
41
/* .IP query
 
42
/*      Query template. Before the query is actually issued, variable
 
43
/*      substitutions are performed. See sqlite_table(5) for details.
 
44
/* .IP result_format
 
45
/*      The format used to expand results from queries.  Substitutions
 
46
/*      are performed as described in sqlite_table(5). Defaults to
 
47
/*      returning the lookup result unchanged.
 
48
/* .IP expansion_limit
 
49
/*      Limit (if any) on the total number of lookup result values.
 
50
/*      Lookups which exceed the limit fail with dict_errno=DICT_ERR_RETRY.
 
51
/*      Note that each non-empty (and non-NULL) column of a
 
52
/*      multi-column result row counts as one result.
 
53
/* .IP "select_field, where_field, additional_conditions"
 
54
/*      Legacy query interface.
 
55
/* SEE ALSO
 
56
/*      dict(3) generic dictionary manager
 
57
/* AUTHOR(S)
 
58
/*      Axel Steiner
 
59
/*      ast@treibsand.com
 
60
/*--*/
 
61
 
 
62
/* System library. */
 
63
 
 
64
#include <sys_defs.h>
 
65
#include <string.h>
 
66
 
 
67
#ifdef HAS_SQLITE
 
68
#include <sqlite3.h>
 
69
 
 
70
#if !defined(SQLITE_VERSION_NUMBER) || (SQLITE_VERSION_NUMBER < 3005004)
 
71
#define sqlite3_prepare_v2 sqlite3_prepare
 
72
#endif
 
73
 
 
74
/* Utility library. */
 
75
 
 
76
#include <msg.h>
 
77
#include <dict.h>
 
78
#include <vstring.h>
 
79
#include <stringops.h>
 
80
#include <mymalloc.h>
 
81
 
 
82
/* Global library. */
 
83
 
 
84
#include <cfg_parser.h>
 
85
#include <db_common.h>
 
86
 
 
87
/* Application-specific. */
 
88
 
 
89
#include <dict_sqlite.h>
 
90
 
 
91
typedef struct {
 
92
    DICT    dict;                       /* generic member */
 
93
    CFG_PARSER *parser;                 /* common parameter parser */
 
94
    sqlite3 *db;                        /* sqlite handle */
 
95
    char   *query;                      /* db_common_expand() query */
 
96
    char   *result_format;              /* db_common_expand() result_format */
 
97
    void   *ctx;                        /* db_common_parse() context */
 
98
    char   *dbpath;                     /* dbpath config attribute */
 
99
    int     expansion_limit;            /* expansion_limit config attribute */
 
100
} DICT_SQLITE;
 
101
 
 
102
/* dict_sqlite_quote - escape SQL metacharacters in input string */
 
103
 
 
104
static void dict_sqlite_quote(DICT *dict, const char *raw_text, VSTRING *result)
 
105
{
 
106
    char   *quoted_text;
 
107
 
 
108
    quoted_text = sqlite3_mprintf("%q", raw_text);
 
109
    /* Fix 20100616 */
 
110
    if (quoted_text == 0)
 
111
        msg_fatal("dict_sqlite_quote: out of memory");
 
112
    vstring_strcat(result, raw_text);
 
113
    sqlite3_free(quoted_text);
 
114
}
 
115
 
 
116
/* dict_sqlite_close - close the database */
 
117
 
 
118
static void dict_sqlite_close(DICT *dict)
 
119
{
 
120
    const char *myname = "dict_sqlite_close";
 
121
    DICT_SQLITE *dict_sqlite = (DICT_SQLITE *) dict;
 
122
 
 
123
    if (msg_verbose)
 
124
        msg_info("%s: %s", myname, dict_sqlite->parser->name);
 
125
 
 
126
    if (sqlite3_close(dict_sqlite->db) != SQLITE_OK)
 
127
        msg_fatal("%s: close %s failed", myname, dict_sqlite->parser->name);
 
128
    cfg_parser_free(dict_sqlite->parser);
 
129
    myfree(dict_sqlite->dbpath);
 
130
    myfree(dict_sqlite->query);
 
131
    myfree(dict_sqlite->result_format);
 
132
    if (dict_sqlite->ctx)
 
133
        db_common_free_ctx(dict_sqlite->ctx);
 
134
    if (dict->fold_buf)
 
135
        vstring_free(dict->fold_buf);
 
136
    dict_free(dict);
 
137
}
 
138
 
 
139
/* dict_sqlite_lookup - find database entry */
 
140
 
 
141
static const char *dict_sqlite_lookup(DICT *dict, const char *name)
 
142
{
 
143
    const char *myname = "dict_sqlite_lookup";
 
144
    DICT_SQLITE *dict_sqlite = (DICT_SQLITE *) dict;
 
145
    sqlite3_stmt *sql_stmt;
 
146
    const char *query_remainder;
 
147
    static VSTRING *query;
 
148
    static VSTRING *result;
 
149
    const char *retval;
 
150
    int     expansion = 0;
 
151
    int     status;
 
152
 
 
153
    /*
 
154
     * Don't frustrate future attempts to make Postfix UTF-8 transparent.
 
155
     */
 
156
    if (!valid_utf_8(name, strlen(name))) {
 
157
        if (msg_verbose)
 
158
            msg_info("%s: %s: Skipping lookup of non-UTF-8 key '%s'",
 
159
                     myname, dict_sqlite->parser->name, name);
 
160
        return (0);
 
161
    }
 
162
 
 
163
    /*
 
164
     * Optionally fold the key. Folding may be enabled on on-the-fly.
 
165
     */
 
166
    if (dict->flags & DICT_FLAG_FOLD_FIX) {
 
167
        if (dict->fold_buf == 0)
 
168
            dict->fold_buf = vstring_alloc(100);
 
169
        vstring_strcpy(dict->fold_buf, name);
 
170
        name = lowercase(vstring_str(dict->fold_buf));
 
171
    }
 
172
 
 
173
    /*
 
174
     * Apply the optional domain filter for email address lookups.
 
175
     */
 
176
    if (db_common_check_domain(dict_sqlite->ctx, name) == 0) {
 
177
        if (msg_verbose)
 
178
            msg_info("%s: %s: Skipping lookup of '%s'",
 
179
                     myname, dict_sqlite->parser->name, name);
 
180
        return (0);
 
181
    }
 
182
 
 
183
    /*
 
184
     * Expand the query and query the database.
 
185
     */
 
186
#define INIT_VSTR(buf, len) do { \
 
187
        if (buf == 0) \
 
188
                buf = vstring_alloc(len); \
 
189
        VSTRING_RESET(buf); \
 
190
        VSTRING_TERMINATE(buf); \
 
191
    } while (0)
 
192
 
 
193
    INIT_VSTR(query, 10);
 
194
 
 
195
    if (!db_common_expand(dict_sqlite->ctx, dict_sqlite->query,
 
196
                          name, 0, query, dict_sqlite_quote))
 
197
        return (0);
 
198
 
 
199
    if (msg_verbose)
 
200
        msg_info("%s: %s: Searching with query %s",
 
201
                 myname, dict_sqlite->parser->name, vstring_str(query));
 
202
 
 
203
    if (sqlite3_prepare_v2(dict_sqlite->db, vstring_str(query), -1,
 
204
                           &sql_stmt, &query_remainder) != SQLITE_OK)
 
205
        msg_fatal("%s: %s: SQL prepare failed: %s\n",
 
206
                  myname, dict_sqlite->parser->name,
 
207
                  sqlite3_errmsg(dict_sqlite->db));
 
208
 
 
209
    if (*query_remainder && msg_verbose)
 
210
        msg_info("%s: %s: Ignoring text at end of query: %s",
 
211
                 myname, dict_sqlite->parser->name, query_remainder);
 
212
 
 
213
    /*
 
214
     * Retrieve and expand the result(s).
 
215
     */
 
216
    INIT_VSTR(result, 10);
 
217
    while ((status = sqlite3_step(sql_stmt)) != SQLITE_DONE) {
 
218
        if (status == SQLITE_ROW) {
 
219
            if (db_common_expand(dict_sqlite->ctx, dict_sqlite->result_format,
 
220
                                 (char *) sqlite3_column_text(sql_stmt, 0),
 
221
                                 name, result, 0)
 
222
                && dict_sqlite->expansion_limit > 0
 
223
                && ++expansion > dict_sqlite->expansion_limit) {
 
224
                msg_warn("%s: %s: Expansion limit exceeded for key '%s'",
 
225
                         myname, dict_sqlite->parser->name, name);
 
226
                dict_errno = DICT_ERR_RETRY;
 
227
                break;
 
228
            }
 
229
        }
 
230
        /* Fix 20100616 */
 
231
        else {
 
232
            msg_warn("%s: %s: SQL step failed for query '%s': %s\n",
 
233
                     myname, dict_sqlite->parser->name,
 
234
                     vstring_str(query), sqlite3_errmsg(dict_sqlite->db));
 
235
            dict_errno = DICT_ERR_RETRY;
 
236
            break;
 
237
        }
 
238
    }
 
239
 
 
240
    /*
 
241
     * Clean up.
 
242
     */
 
243
    if (sqlite3_finalize(sql_stmt))
 
244
        msg_fatal("%s: %s: SQL finalize failed for query '%s': %s\n",
 
245
                  myname, dict_sqlite->parser->name,
 
246
                  vstring_str(query), sqlite3_errmsg(dict_sqlite->db));
 
247
 
 
248
    return ((dict_errno == 0 && *(retval = vstring_str(result)) != 0) ?
 
249
            retval : 0);
 
250
}
 
251
 
 
252
/* sqlite_parse_config - parse sqlite configuration file */
 
253
 
 
254
static void sqlite_parse_config(DICT_SQLITE *dict_sqlite, const char *sqlitecf)
 
255
{
 
256
    VSTRING *buf;
 
257
 
 
258
    /*
 
259
     * Parse the primary configuration parameters, and emulate the legacy
 
260
     * query interface if necessary. This simplifies migration from one SQL
 
261
     * database type to another.
 
262
     */
 
263
    dict_sqlite->parser = cfg_parser_alloc(sqlitecf);
 
264
    dict_sqlite->dbpath = cfg_get_str(dict_sqlite->parser, "dbpath", "", 1, 0);
 
265
    dict_sqlite->query = cfg_get_str(dict_sqlite->parser, "query", NULL, 0, 0);
 
266
    if (dict_sqlite->query == 0) {
 
267
        buf = vstring_alloc(100);
 
268
        db_common_sql_build_query(buf, dict_sqlite->parser);
 
269
        dict_sqlite->query = vstring_export(buf);
 
270
    }
 
271
    dict_sqlite->result_format =
 
272
        cfg_get_str(dict_sqlite->parser, "result_format", "%s", 1, 0);
 
273
    dict_sqlite->expansion_limit =
 
274
        cfg_get_int(dict_sqlite->parser, "expansion_limit", 0, 0, 0);
 
275
 
 
276
    /*
 
277
     * Parse the query / result templates and the optional domain filter.
 
278
     */
 
279
    dict_sqlite->ctx = 0;
 
280
    (void) db_common_parse(&dict_sqlite->dict, &dict_sqlite->ctx,
 
281
                           dict_sqlite->query, 1);
 
282
    (void) db_common_parse(0, &dict_sqlite->ctx, dict_sqlite->result_format, 0);
 
283
    db_common_parse_domain(dict_sqlite->parser, dict_sqlite->ctx);
 
284
 
 
285
    /*
 
286
     * Maps that use substring keys should only be used with the full input
 
287
     * key.
 
288
     */
 
289
    if (db_common_dict_partial(dict_sqlite->ctx))
 
290
        dict_sqlite->dict.flags |= DICT_FLAG_PATTERN;
 
291
    else
 
292
        dict_sqlite->dict.flags |= DICT_FLAG_FIXED;
 
293
}
 
294
 
 
295
/* dict_sqlite_open - open sqlite database */
 
296
 
 
297
DICT   *dict_sqlite_open(const char *name, int open_flags, int dict_flags)
 
298
{
 
299
    DICT_SQLITE *dict_sqlite;
 
300
 
 
301
    /*
 
302
     * Sanity checks.
 
303
     */
 
304
    if (open_flags != O_RDONLY)
 
305
        msg_fatal("%s:%s map requires O_RDONLY access mode",
 
306
                  DICT_TYPE_SQLITE, name);
 
307
 
 
308
    dict_sqlite = (DICT_SQLITE *) dict_alloc(DICT_TYPE_SQLITE, name,
 
309
                                             sizeof(DICT_SQLITE));
 
310
    dict_sqlite->dict.lookup = dict_sqlite_lookup;
 
311
    dict_sqlite->dict.close = dict_sqlite_close;
 
312
    dict_sqlite->dict.flags = dict_flags;
 
313
 
 
314
    sqlite_parse_config(dict_sqlite, name);
 
315
 
 
316
    if (sqlite3_open(dict_sqlite->dbpath, &dict_sqlite->db))
 
317
        msg_fatal("%s:%s: Can't open database: %s\n",
 
318
                  DICT_TYPE_SQLITE, name, sqlite3_errmsg(dict_sqlite->db));
 
319
 
 
320
    return (DICT_DEBUG (&dict_sqlite->dict));
 
321
}
 
322
 
 
323
#endif