Commit | Line | Data |
---|---|---|
13b685f9 PH |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
f9ba5e22 | 5 | /* Copyright (c) University of Cambridge 1995 - 2018 */ |
13b685f9 PH |
6 | /* See the file NOTICE for conditions of use and distribution. */ |
7 | ||
8 | #include "../exim.h" | |
9 | #include "lf_functions.h" | |
13b685f9 | 10 | |
13b685f9 PH |
11 | #include <sqlite3.h> |
12 | ||
13 | ||
14 | /************************************************* | |
15 | * Open entry point * | |
16 | *************************************************/ | |
17 | ||
18 | /* See local README for interface description. */ | |
19 | ||
e6d225ae | 20 | static void * |
13b685f9 PH |
21 | sqlite_open(uschar *filename, uschar **errmsg) |
22 | { | |
23 | sqlite3 *db = NULL; | |
24 | int ret; | |
25 | ||
5903c6ff | 26 | ret = sqlite3_open(CS filename, &db); |
13b685f9 PH |
27 | if (ret != 0) |
28 | { | |
29 | *errmsg = (void *)sqlite3_errmsg(db); | |
42c7f0b4 | 30 | debug_printf_indent("Error opening database: %s\n", *errmsg); |
13b685f9 PH |
31 | } |
32 | ||
31480e42 | 33 | sqlite3_busy_timeout(db, 1000 * sqlite_lock_timeout); |
13b685f9 PH |
34 | return db; |
35 | } | |
36 | ||
37 | ||
38 | /************************************************* | |
39 | * Find entry point * | |
40 | *************************************************/ | |
41 | ||
42 | /* See local README for interface description. */ | |
43 | ||
acec9514 JH |
44 | static int |
45 | sqlite_callback(void *arg, int argc, char **argv, char **azColName) | |
13b685f9 | 46 | { |
acec9514 | 47 | gstring * res = *(gstring **)arg; |
13b685f9 PH |
48 | |
49 | /* For second and subsequent results, insert \n */ | |
50 | ||
acec9514 JH |
51 | if (res) |
52 | res = string_catn(res, US"\n", 1); | |
13b685f9 PH |
53 | |
54 | if (argc > 1) | |
55 | { | |
56 | /* For multiple fields, include the field name too */ | |
d7978c0f | 57 | for (int i = 0; i < argc; i++) |
13b685f9 PH |
58 | { |
59 | uschar *value = US((argv[i] != NULL)? argv[i]:"<NULL>"); | |
acec9514 | 60 | res = lf_quote(US azColName[i], value, Ustrlen(value), res); |
13b685f9 PH |
61 | } |
62 | } | |
63 | ||
64 | else | |
acec9514 | 65 | res = string_cat(res, argv[0] ? US argv[0] : US "<NULL>"); |
13b685f9 | 66 | |
acec9514 | 67 | *(gstring **)arg = res; |
13b685f9 PH |
68 | return 0; |
69 | } | |
70 | ||
71 | ||
e6d225ae | 72 | static int |
55414b25 | 73 | sqlite_find(void *handle, uschar *filename, const uschar *query, int length, |
14b3c5bc | 74 | uschar **result, uschar **errmsg, uint *do_cache) |
13b685f9 PH |
75 | { |
76 | int ret; | |
acec9514 | 77 | gstring * res = NULL; |
13b685f9 | 78 | |
5903c6ff | 79 | ret = sqlite3_exec(handle, CS query, sqlite_callback, &res, (char **)errmsg); |
13b685f9 PH |
80 | if (ret != SQLITE_OK) |
81 | { | |
42c7f0b4 | 82 | debug_printf_indent("sqlite3_exec failed: %s\n", *errmsg); |
13b685f9 PH |
83 | return FAIL; |
84 | } | |
85 | ||
acec9514 | 86 | if (!res) *do_cache = 0; |
13b685f9 | 87 | |
acec9514 | 88 | *result = string_from_gstring(res); |
13b685f9 PH |
89 | return OK; |
90 | } | |
91 | ||
92 | ||
93 | ||
94 | /************************************************* | |
95 | * Close entry point * | |
96 | *************************************************/ | |
97 | ||
98 | /* See local README for interface description. */ | |
99 | ||
e6d225ae | 100 | static void sqlite_close(void *handle) |
13b685f9 PH |
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 | ||
e6d225ae | 121 | static uschar * |
13b685f9 PH |
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; | |
f3ebb786 | 134 | t = quoted = store_get(Ustrlen(s) + count + 1, is_tainted(s)); |
13b685f9 PH |
135 | |
136 | while ((c = *s++) != 0) | |
137 | { | |
138 | if (c == '\'') *t++ = '\''; | |
139 | *t++ = c; | |
140 | } | |
141 | ||
142 | *t = 0; | |
143 | return quoted; | |
144 | } | |
145 | ||
6545de78 PP |
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 | ||
e6d225ae DW |
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 */ | |
6545de78 PP |
175 | sqlite_quote, /* quoting function */ |
176 | sqlite_version_report /* version reporting */ | |
e6d225ae DW |
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 }; | |
13b685f9 PH |
185 | |
186 | /* End of lookups/sqlite.c */ |