1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* Copyright (c) The Exim Maintainers 2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
10 #include "lf_functions.h"
15 /*************************************************
17 *************************************************/
19 /* See local README for interface description. */
22 sqlite_open(const uschar * filename, uschar ** errmsg)
27 if (!filename || !*filename)
29 DEBUG(D_lookup) debug_printf_indent("Using sqlite_dbfile: %s\n", sqlite_dbfile);
30 filename = sqlite_dbfile;
32 if (!filename || *filename != '/')
33 *errmsg = US"absolute file name expected for \"sqlite\" lookup";
34 else if ((ret = sqlite3_open(CCS filename, &db)) != 0)
36 *errmsg = (void *)sqlite3_errmsg(db);
39 DEBUG(D_lookup) debug_printf_indent("Error opening database: %s\n", *errmsg);
43 sqlite3_busy_timeout(db, 1000 * sqlite_lock_timeout);
48 /*************************************************
50 *************************************************/
52 /* See local README for interface description. */
55 sqlite_callback(void *arg, int argc, char **argv, char **azColName)
57 gstring * res = *(gstring **)arg;
59 /* For second and subsequent results, insert \n */
62 res = string_catn(res, US"\n", 1);
66 /* For multiple fields, include the field name too */
67 for (int i = 0; i < argc; i++)
69 uschar *value = US((argv[i] != NULL)? argv[i]:"<NULL>");
70 res = lf_quote(US azColName[i], value, Ustrlen(value), res);
75 res = string_cat(res, argv[0] ? US argv[0] : US "<NULL>");
77 *(gstring **)arg = res;
83 sqlite_find(void * handle, const uschar * filename, const uschar * query,
84 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
90 ret = sqlite3_exec(handle, CS query, sqlite_callback, &res, CSS errmsg);
93 debug_printf_indent("sqlite3_exec failed: %s\n", *errmsg);
97 if (!res) *do_cache = 0;
99 *result = string_from_gstring(res);
105 /*************************************************
106 * Close entry point *
107 *************************************************/
109 /* See local README for interface description. */
111 static void sqlite_close(void *handle)
113 sqlite3_close(handle);
118 /*************************************************
119 * Quote entry point *
120 *************************************************/
122 /* From what I have found so far, the only character that needs to be quoted
123 for sqlite is the single quote, and it is quoted by doubling.
126 s the string to be quoted
127 opt additional option text or NULL if none
129 Returns: the processed string or NULL for a bad option
133 sqlite_quote(uschar *s, uschar *opt)
140 if (opt != NULL) return NULL; /* No options recognized */
142 while ((c = *t++) != 0) if (c == '\'') count++;
144 if (count == 0) return s;
145 t = quoted = store_get(Ustrlen(s) + count + 1, is_tainted(s));
147 while ((c = *s++) != 0)
149 if (c == '\'') *t++ = '\'';
159 /*************************************************
160 * Version reporting entry point *
161 *************************************************/
163 /* See local README for interface description. */
165 #include "../version.h"
168 sqlite_version_report(FILE *f)
170 fprintf(f, "Library version: SQLite: Compile: %s\n"
172 SQLITE_VERSION, sqlite3_libversion());
174 fprintf(f, " Exim version %s\n", EXIM_VERSION_STR);
178 static lookup_info _lookup_info = {
179 .name = US"sqlite", /* lookup name */
180 .type = lookup_absfilequery, /* query-style lookup, starts with file name */
181 .open = sqlite_open, /* open function */
182 .check = NULL, /* no check function */
183 .find = sqlite_find, /* find function */
184 .close = sqlite_close, /* close function */
185 .tidy = NULL, /* no tidy function */
186 .quote = sqlite_quote, /* quoting function */
187 .version_report = sqlite_version_report /* version reporting */
191 #define sqlite_lookup_module_info _lookup_module_info
194 static lookup_info *_lookup_list[] = { &_lookup_info };
195 lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
197 /* End of lookups/sqlite.c */