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