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 *
sqlite_open(const uschar * filename,uschar ** errmsg)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
sqlite_callback(void * arg,int argc,char ** argv,char ** azColName)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] != NULL)? 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 *(gstring **)arg = res;
78 return 0;
79 }
80 
81 
82 static int
sqlite_find(void * handle,const uschar * filename,const uschar * query,int length,uschar ** result,uschar ** errmsg,uint * do_cache,const uschar * opts)83 sqlite_find(void * handle, const uschar * filename, const uschar * query,
84   int length, uschar ** result, uschar ** errmsg, uint * do_cache,
85   const uschar * opts)
86 {
87 int ret;
88 gstring * res = NULL;
89 
90 ret = sqlite3_exec(handle, CS query, sqlite_callback, &res, CSS errmsg);
91 if (ret != SQLITE_OK)
92   {
93   debug_printf_indent("sqlite3_exec failed: %s\n", *errmsg);
94   return FAIL;
95   }
96 
97 if (!res) *do_cache = 0;
98 
99 *result = string_from_gstring(res);
100 return OK;
101 }
102 
103 
104 
105 /*************************************************
106 *               Close entry point                *
107 *************************************************/
108 
109 /* See local README for interface description. */
110 
sqlite_close(void * handle)111 static void sqlite_close(void *handle)
112 {
113 sqlite3_close(handle);
114 }
115 
116 
117 
118 /*************************************************
119 *               Quote entry point                *
120 *************************************************/
121 
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.
124 
125 Arguments:
126   s          the string to be quoted
127   opt        additional option text or NULL if none
128 
129 Returns:     the processed string or NULL for a bad option
130 */
131 
132 static uschar *
sqlite_quote(uschar * s,uschar * opt)133 sqlite_quote(uschar *s, uschar *opt)
134 {
135 register int c;
136 int count = 0;
137 uschar *t = s;
138 uschar *quoted;
139 
140 if (opt != NULL) return NULL;     /* No options recognized */
141 
142 while ((c = *t++) != 0) if (c == '\'') count++;
143 
144 if (count == 0) return s;
145 t = quoted = store_get(Ustrlen(s) + count + 1, is_tainted(s));
146 
147 while ((c = *s++) != 0)
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 void
sqlite_version_report(FILE * f)168 sqlite_version_report(FILE *f)
169 {
170 fprintf(f, "Library version: SQLite: Compile: %s\n"
171            "                         Runtime: %s\n",
172         SQLITE_VERSION, sqlite3_libversion());
173 #ifdef DYNLOOKUP
174 fprintf(f, "                         Exim version %s\n", EXIM_VERSION_STR);
175 #endif
176 }
177 
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 */
188 };
189 
190 #ifdef DYNLOOKUP
191 #define sqlite_lookup_module_info _lookup_module_info
192 #endif
193 
194 static lookup_info *_lookup_list[] = { &_lookup_info };
195 lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
196 
197 /* End of lookups/sqlite.c */
198