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