1 /* gpgsql.c - SQLite helper functions.
2 * Copyright (C) 2015 g10 Code GmbH
3 *
4 * This file is part of GnuPG.
5 *
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include <config.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "gpg.h"
26 #include "../common/util.h"
27 #include "../common/logging.h"
28
29 #include "gpgsql.h"
30
31 /* This is a convenience function that combines sqlite3_mprintf and
32 sqlite3_exec. */
33 int
gpgsql_exec_printf(sqlite3 * db,int (* callback)(void *,int,char **,char **),void * cookie,char ** errmsg,const char * sql,...)34 gpgsql_exec_printf (sqlite3 *db,
35 int (*callback)(void*,int,char**,char**), void *cookie,
36 char **errmsg,
37 const char *sql, ...)
38 {
39 va_list ap;
40 int rc;
41 char *sql2;
42
43 va_start (ap, sql);
44 sql2 = sqlite3_vmprintf (sql, ap);
45 va_end (ap);
46
47 #if 0
48 log_debug ("tofo db: executing: '%s'\n", sql2);
49 #endif
50
51 rc = sqlite3_exec (db, sql2, callback, cookie, errmsg);
52
53 sqlite3_free (sql2);
54
55 return rc;
56 }
57
58 int
gpgsql_stepx(sqlite3 * db,sqlite3_stmt ** stmtp,gpgsql_stepx_callback callback,void * cookie,char ** errmsg,const char * sql,...)59 gpgsql_stepx (sqlite3 *db,
60 sqlite3_stmt **stmtp,
61 gpgsql_stepx_callback callback,
62 void *cookie,
63 char **errmsg,
64 const char *sql, ...)
65 {
66 int rc;
67 int err = 0;
68 sqlite3_stmt *stmt = NULL;
69
70 va_list va;
71 int args;
72 enum gpgsql_arg_type t;
73 int i;
74
75 int cols;
76 /* Names of the columns. We initialize this lazily to avoid the
77 overhead in case the query doesn't return any results. */
78 const char **azColName = 0;
79 int callback_initialized = 0;
80
81 const char **azVals = 0;
82
83 callback_initialized = 0;
84
85 if (stmtp && *stmtp)
86 {
87 stmt = *stmtp;
88
89 /* Make sure this statement is associated with the supplied db. */
90 log_assert (db == sqlite3_db_handle (stmt));
91
92 #if DEBUG_TOFU_CACHE
93 prepares_saved ++;
94 #endif
95 }
96 else
97 {
98 const char *tail = NULL;
99
100 rc = sqlite3_prepare_v2 (db, sql, -1, &stmt, &tail);
101 if (rc)
102 log_fatal ("failed to prepare SQL: %s", sql);
103
104 /* We can only process a single statement. */
105 if (tail)
106 {
107 while (*tail == ' ' || *tail == ';' || *tail == '\n')
108 tail ++;
109
110 if (*tail)
111 log_fatal
112 ("sqlite3_stepx can only process a single SQL statement."
113 " Second statement starts with: '%s'\n",
114 tail);
115 }
116
117 if (stmtp)
118 *stmtp = stmt;
119 }
120
121 #if DEBUG_TOFU_CACHE
122 queries ++;
123 #endif
124
125 args = sqlite3_bind_parameter_count (stmt);
126 va_start (va, sql);
127 if (args)
128 {
129 for (i = 1; i <= args; i ++)
130 {
131 t = va_arg (va, enum gpgsql_arg_type);
132 switch (t)
133 {
134 case GPGSQL_ARG_INT:
135 {
136 int value = va_arg (va, int);
137 err = sqlite3_bind_int (stmt, i, value);
138 break;
139 }
140 case GPGSQL_ARG_LONG_LONG:
141 {
142 long long value = va_arg (va, long long);
143 err = sqlite3_bind_int64 (stmt, i, value);
144 break;
145 }
146 case GPGSQL_ARG_STRING:
147 {
148 char *text = va_arg (va, char *);
149 err = sqlite3_bind_text (stmt, i, text, -1, SQLITE_STATIC);
150 break;
151 }
152 case GPGSQL_ARG_BLOB:
153 {
154 char *blob = va_arg (va, void *);
155 long long length = va_arg (va, long long);
156 err = sqlite3_bind_blob (stmt, i, blob, length, SQLITE_STATIC);
157 break;
158 }
159 default:
160 /* Internal error. Likely corruption. */
161 log_fatal ("Bad value for parameter type %d.\n", t);
162 }
163
164 if (err)
165 {
166 log_fatal ("Error binding parameter %d\n", i);
167 goto out;
168 }
169 }
170
171 }
172 t = va_arg (va, enum gpgsql_arg_type);
173 log_assert (t == GPGSQL_ARG_END);
174 va_end (va);
175
176 for (;;)
177 {
178 rc = sqlite3_step (stmt);
179
180 if (rc != SQLITE_ROW)
181 /* No more data (SQLITE_DONE) or an error occurred. */
182 break;
183
184 if (! callback)
185 continue;
186
187 if (! callback_initialized)
188 {
189 cols = sqlite3_column_count (stmt);
190 azColName = xmalloc (2 * cols * sizeof (const char *) + 1);
191
192 for (i = 0; i < cols; i ++)
193 azColName[i] = sqlite3_column_name (stmt, i);
194
195 callback_initialized = 1;
196 }
197
198 azVals = &azColName[cols];
199 for (i = 0; i < cols; i ++)
200 {
201 azVals[i] = sqlite3_column_text (stmt, i);
202 if (! azVals[i] && sqlite3_column_type (stmt, i) != SQLITE_NULL)
203 /* Out of memory. */
204 {
205 err = SQLITE_NOMEM;
206 break;
207 }
208 }
209
210 if (callback (cookie, cols, (char **) azVals, (char **) azColName, stmt))
211 /* A non-zero result means to abort. */
212 {
213 err = SQLITE_ABORT;
214 break;
215 }
216 }
217
218 out:
219 xfree (azColName);
220
221 if (stmtp)
222 rc = sqlite3_reset (stmt);
223 else
224 rc = sqlite3_finalize (stmt);
225 if (rc == SQLITE_OK && err)
226 /* Local error. */
227 {
228 rc = err;
229 if (errmsg)
230 {
231 const char *e = sqlite3_errstr (err);
232 size_t l = strlen (e) + 1;
233 *errmsg = sqlite3_malloc (l);
234 if (! *errmsg)
235 log_fatal ("Out of memory.\n");
236 memcpy (*errmsg, e, l);
237 }
238 }
239 else if (rc != SQLITE_OK && errmsg)
240 /* Error reported by sqlite. */
241 {
242 const char * e = sqlite3_errmsg (db);
243 size_t l = strlen (e) + 1;
244 *errmsg = sqlite3_malloc (l);
245 if (! *errmsg)
246 log_fatal ("Out of memory.\n");
247 memcpy (*errmsg, e, l);
248 }
249
250 return rc;
251 }
252