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