Commit | Line | Data |
---|---|---|
13b685f9 PH |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
f9ba5e22 | 5 | /* Copyright (c) University of Cambridge 1995 - 2018 */ |
13b685f9 PH |
6 | /* See the file NOTICE for conditions of use and distribution. */ |
7 | ||
8 | #include "../exim.h" | |
9 | #include "lf_functions.h" | |
13b685f9 | 10 | |
13b685f9 PH |
11 | #include <sqlite3.h> |
12 | ||
13 | ||
14 | /************************************************* | |
15 | * Open entry point * | |
16 | *************************************************/ | |
17 | ||
18 | /* See local README for interface description. */ | |
19 | ||
e6d225ae | 20 | static void * |
d447dbd1 | 21 | sqlite_open(const uschar * filename, uschar ** errmsg) |
13b685f9 PH |
22 | { |
23 | sqlite3 *db = NULL; | |
24 | int ret; | |
25 | ||
298849d8 JH |
26 | if (!filename || !*filename) filename = sqlite_dbfile; |
27 | if (*filename != '/') | |
28 | *errmsg = US"absolute file name expected for \"sqlite\" lookup"; | |
29 | else if ((ret = sqlite3_open(CCS filename, &db)) != 0) | |
13b685f9 PH |
30 | { |
31 | *errmsg = (void *)sqlite3_errmsg(db); | |
298849d8 | 32 | DEBUG(D_lookup) debug_printf_indent("Error opening database: %s\n", *errmsg); |
13b685f9 PH |
33 | } |
34 | ||
31480e42 | 35 | sqlite3_busy_timeout(db, 1000 * sqlite_lock_timeout); |
13b685f9 PH |
36 | return db; |
37 | } | |
38 | ||
39 | ||
40 | /************************************************* | |
41 | * Find entry point * | |
42 | *************************************************/ | |
43 | ||
44 | /* See local README for interface description. */ | |
45 | ||
acec9514 JH |
46 | static int |
47 | sqlite_callback(void *arg, int argc, char **argv, char **azColName) | |
13b685f9 | 48 | { |
acec9514 | 49 | gstring * res = *(gstring **)arg; |
13b685f9 PH |
50 | |
51 | /* For second and subsequent results, insert \n */ | |
52 | ||
acec9514 JH |
53 | if (res) |
54 | res = string_catn(res, US"\n", 1); | |
13b685f9 PH |
55 | |
56 | if (argc > 1) | |
57 | { | |
58 | /* For multiple fields, include the field name too */ | |
d7978c0f | 59 | for (int i = 0; i < argc; i++) |
13b685f9 PH |
60 | { |
61 | uschar *value = US((argv[i] != NULL)? argv[i]:"<NULL>"); | |
acec9514 | 62 | res = lf_quote(US azColName[i], value, Ustrlen(value), res); |
13b685f9 PH |
63 | } |
64 | } | |
65 | ||
66 | else | |
acec9514 | 67 | res = string_cat(res, argv[0] ? US argv[0] : US "<NULL>"); |
13b685f9 | 68 | |
acec9514 | 69 | *(gstring **)arg = res; |
13b685f9 PH |
70 | return 0; |
71 | } | |
72 | ||
73 | ||
e6d225ae | 74 | static int |
d447dbd1 | 75 | sqlite_find(void * handle, const uschar * filename, const uschar * query, |
67a57a5a JH |
76 | int length, uschar ** result, uschar ** errmsg, uint * do_cache, |
77 | const uschar * opts) | |
13b685f9 PH |
78 | { |
79 | int ret; | |
acec9514 | 80 | gstring * res = NULL; |
13b685f9 | 81 | |
137ae145 | 82 | ret = sqlite3_exec(handle, CS query, sqlite_callback, &res, CSS errmsg); |
13b685f9 PH |
83 | if (ret != SQLITE_OK) |
84 | { | |
42c7f0b4 | 85 | debug_printf_indent("sqlite3_exec failed: %s\n", *errmsg); |
13b685f9 PH |
86 | return FAIL; |
87 | } | |
88 | ||
acec9514 | 89 | if (!res) *do_cache = 0; |
13b685f9 | 90 | |
acec9514 | 91 | *result = string_from_gstring(res); |
13b685f9 PH |
92 | return OK; |
93 | } | |
94 | ||
95 | ||
96 | ||
97 | /************************************************* | |
98 | * Close entry point * | |
99 | *************************************************/ | |
100 | ||
101 | /* See local README for interface description. */ | |
102 | ||
e6d225ae | 103 | static void sqlite_close(void *handle) |
13b685f9 PH |
104 | { |
105 | sqlite3_close(handle); | |
106 | } | |
107 | ||
108 | ||
109 | ||
110 | /************************************************* | |
111 | * Quote entry point * | |
112 | *************************************************/ | |
113 | ||
114 | /* From what I have found so far, the only character that needs to be quoted | |
115 | for sqlite is the single quote, and it is quoted by doubling. | |
116 | ||
117 | Arguments: | |
118 | s the string to be quoted | |
119 | opt additional option text or NULL if none | |
120 | ||
121 | Returns: the processed string or NULL for a bad option | |
122 | */ | |
123 | ||
e6d225ae | 124 | static uschar * |
13b685f9 PH |
125 | sqlite_quote(uschar *s, uschar *opt) |
126 | { | |
127 | register int c; | |
128 | int count = 0; | |
129 | uschar *t = s; | |
130 | uschar *quoted; | |
131 | ||
132 | if (opt != NULL) return NULL; /* No options recognized */ | |
133 | ||
134 | while ((c = *t++) != 0) if (c == '\'') count++; | |
135 | ||
136 | if (count == 0) return s; | |
f3ebb786 | 137 | t = quoted = store_get(Ustrlen(s) + count + 1, is_tainted(s)); |
13b685f9 PH |
138 | |
139 | while ((c = *s++) != 0) | |
140 | { | |
141 | if (c == '\'') *t++ = '\''; | |
142 | *t++ = c; | |
143 | } | |
144 | ||
145 | *t = 0; | |
146 | return quoted; | |
147 | } | |
148 | ||
6545de78 PP |
149 | |
150 | ||
151 | /************************************************* | |
152 | * Version reporting entry point * | |
153 | *************************************************/ | |
154 | ||
155 | /* See local README for interface description. */ | |
156 | ||
157 | #include "../version.h" | |
158 | ||
159 | void | |
160 | sqlite_version_report(FILE *f) | |
161 | { | |
162 | fprintf(f, "Library version: SQLite: Compile: %s\n" | |
163 | " Runtime: %s\n", | |
164 | SQLITE_VERSION, sqlite3_libversion()); | |
165 | #ifdef DYNLOOKUP | |
166 | fprintf(f, " Exim version %s\n", EXIM_VERSION_STR); | |
167 | #endif | |
168 | } | |
169 | ||
e6d225ae | 170 | static lookup_info _lookup_info = { |
9f400174 JH |
171 | .name = US"sqlite", /* lookup name */ |
172 | .type = lookup_absfilequery, /* query-style lookup, starts with file name */ | |
173 | .open = sqlite_open, /* open function */ | |
174 | .check = NULL, /* no check function */ | |
175 | .find = sqlite_find, /* find function */ | |
176 | .close = sqlite_close, /* close function */ | |
177 | .tidy = NULL, /* no tidy function */ | |
178 | .quote = sqlite_quote, /* quoting function */ | |
179 | .version_report = sqlite_version_report /* version reporting */ | |
e6d225ae DW |
180 | }; |
181 | ||
182 | #ifdef DYNLOOKUP | |
183 | #define sqlite_lookup_module_info _lookup_module_info | |
184 | #endif | |
185 | ||
186 | static lookup_info *_lookup_list[] = { &_lookup_info }; | |
187 | lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 }; | |
13b685f9 PH |
188 | |
189 | /* End of lookups/sqlite.c */ |