vb@1517
|
1 |
// This file is under GNU General Public License 3.0
|
vb@1517
|
2 |
// see LICENSE.txt
|
vb@1517
|
3 |
|
vb@125
|
4 |
#include "pEp_internal.h"
|
vb@98
|
5 |
#include "dynamic_api.h"
|
vb@28
|
6 |
#include "cryptotech.h"
|
vb@28
|
7 |
#include "transport.h"
|
vb@515
|
8 |
#include "blacklist.h"
|
vb@2880
|
9 |
#include "KeySync_fsm.h"
|
vb@0
|
10 |
|
krista@2176
|
11 |
#include <time.h>
|
krista@2176
|
12 |
#include <stdlib.h>
|
krista@2176
|
13 |
|
krista@3539
|
14 |
#define _PEP_SQLITE_DEBUG 0
|
krista@2488
|
15 |
#if _PEP_SQLITE_DEBUG
|
krista@2487
|
16 |
#include <sqlite3.h>
|
krista@2487
|
17 |
#endif
|
krista@2480
|
18 |
|
krista@2125
|
19 |
static volatile int init_count = -1;
|
vb@62
|
20 |
|
krista@1884
|
21 |
// sql overloaded functions - modified from sqlite3.c
|
krista@1884
|
22 |
static void _sql_lower(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
|
krista@1884
|
23 |
const char *z2;
|
vb@3090
|
24 |
int n;
|
krista@1884
|
25 |
z2 = (char*)sqlite3_value_text(argv[0]);
|
krista@1884
|
26 |
n = sqlite3_value_bytes(argv[0]);
|
krista@1884
|
27 |
/* Verify that the call to _bytes() does not invalidate the _text() pointer */
|
krista@1884
|
28 |
assert( z2==(char*)sqlite3_value_text(argv[0]) );
|
krista@1884
|
29 |
if( z2 ){
|
vb@3090
|
30 |
char *z1 = (char*)sqlite3_malloc(n+1);
|
krista@1884
|
31 |
if( z1 ){
|
vb@3090
|
32 |
for(int i=0; i<n; i++){
|
krista@1884
|
33 |
char c = z2[i];
|
krista@1884
|
34 |
char c_mod = c | 0x20;
|
krista@1884
|
35 |
if (c_mod < 0x61 || c_mod > 0x7a)
|
krista@1884
|
36 |
c_mod = c;
|
krista@1884
|
37 |
z1[i] = c_mod;
|
krista@1884
|
38 |
}
|
krista@1884
|
39 |
z1[n] = '\0';
|
krista@1884
|
40 |
sqlite3_result_text(ctx, z1, n, sqlite3_free);
|
krista@1884
|
41 |
}
|
krista@1884
|
42 |
}
|
krista@1884
|
43 |
}
|
krista@1884
|
44 |
|
krista@2480
|
45 |
#if _PEP_SQLITE_DEBUG
|
krista@2479
|
46 |
int sql_trace_callback (unsigned trace_constant,
|
krista@2479
|
47 |
void* context_ptr,
|
krista@2479
|
48 |
void* P,
|
krista@2479
|
49 |
void* X) {
|
krista@2479
|
50 |
switch (trace_constant) {
|
krista@2479
|
51 |
case SQLITE_TRACE_STMT:
|
krista@2479
|
52 |
fprintf(stderr, "SQL_DEBUG: STMT - ");
|
krista@2479
|
53 |
const char* X_str = (const char*) X;
|
krista@2479
|
54 |
if (!EMPTYSTR(X_str) && X_str[0] == '-' && X_str[1] == '-')
|
krista@2479
|
55 |
fprintf(stderr, "%s\n", X_str);
|
krista@2479
|
56 |
else
|
krista@2479
|
57 |
fprintf(stderr, "%s\n", sqlite3_expanded_sql((sqlite3_stmt*)P));
|
krista@2479
|
58 |
break;
|
krista@2479
|
59 |
case SQLITE_TRACE_ROW:
|
krista@2479
|
60 |
fprintf(stderr, "SQL_DEBUG: ROW - ");
|
krista@2479
|
61 |
fprintf(stderr, "%s\n", sqlite3_expanded_sql((sqlite3_stmt*)P));
|
krista@2479
|
62 |
break;
|
krista@2479
|
63 |
case SQLITE_TRACE_CLOSE:
|
krista@2479
|
64 |
fprintf(stderr, "SQL_DEBUG: CLOSE - ");
|
krista@2479
|
65 |
break;
|
krista@2479
|
66 |
default:
|
krista@2479
|
67 |
break;
|
krista@2479
|
68 |
}
|
krista@2479
|
69 |
return 0;
|
krista@2479
|
70 |
}
|
krista@2480
|
71 |
#endif
|
krista@1884
|
72 |
|
edouard@1518
|
73 |
// sql manipulation statements
|
edouard@1518
|
74 |
static const char *sql_log =
|
edouard@1518
|
75 |
"insert into log (title, entity, description, comment)"
|
edouard@1518
|
76 |
"values (?1, ?2, ?3, ?4);";
|
edouard@1518
|
77 |
|
edouard@1518
|
78 |
static const char *sql_trustword =
|
edouard@1518
|
79 |
"select id, word from wordlist where lang = lower(?1) "
|
edouard@1518
|
80 |
"and id = ?2 ;";
|
edouard@1518
|
81 |
|
krista@3350
|
82 |
// FIXME?: problems if we don't have a key for the user - we get nothing
|
edouard@1518
|
83 |
static const char *sql_get_identity =
|
edouard@1518
|
84 |
"select fpr, username, comm_type, lang,"
|
krista@2461
|
85 |
" identity.flags | pgp_keypair.flags,"
|
krista@3730
|
86 |
" is_own, pEp_version_major, pEp_version_minor"
|
edouard@1518
|
87 |
" from identity"
|
edouard@1518
|
88 |
" join person on id = identity.user_id"
|
edouard@1518
|
89 |
" join pgp_keypair on fpr = identity.main_key_id"
|
edouard@1518
|
90 |
" join trust on id = trust.user_id"
|
krista@1884
|
91 |
" and pgp_keypair_fpr = identity.main_key_id"
|
krista@1884
|
92 |
" where (case when (address = ?1) then (1)"
|
krista@1884
|
93 |
" when (lower(address) = lower(?1)) then (1)"
|
krista@1884
|
94 |
" when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
|
krista@1884
|
95 |
" else 0"
|
krista@1884
|
96 |
" end) = 1"
|
krista@2505
|
97 |
" and identity.user_id = ?2"
|
krista@2506
|
98 |
" order by is_own desc, "
|
krista@2506
|
99 |
" timestamp desc; ";
|
edouard@1518
|
100 |
|
krista@2893
|
101 |
static const char *sql_get_identities_by_main_key_id =
|
krista@2917
|
102 |
"select address, identity.user_id, username, comm_type, lang,"
|
krista@2893
|
103 |
" identity.flags | pgp_keypair.flags,"
|
krista@3730
|
104 |
" is_own, pEp_version_major, pEp_version_minor"
|
krista@2893
|
105 |
" from identity"
|
krista@2893
|
106 |
" join person on id = identity.user_id"
|
krista@2893
|
107 |
" join pgp_keypair on fpr = identity.main_key_id"
|
krista@2893
|
108 |
" join trust on id = trust.user_id"
|
krista@2893
|
109 |
" and pgp_keypair_fpr = identity.main_key_id"
|
krista@2917
|
110 |
" where identity.main_key_id = ?1"
|
krista@2893
|
111 |
" order by is_own desc, "
|
krista@2893
|
112 |
" timestamp desc; ";
|
krista@2893
|
113 |
|
krista@2461
|
114 |
static const char *sql_get_identity_without_trust_check =
|
krista@2461
|
115 |
"select identity.main_key_id, username, lang,"
|
krista@3730
|
116 |
" identity.flags, is_own, pEp_version_major, pEp_version_minor"
|
krista@2461
|
117 |
" from identity"
|
krista@2461
|
118 |
" join person on id = identity.user_id"
|
krista@2461
|
119 |
" where (case when (address = ?1) then (1)"
|
krista@2461
|
120 |
" when (lower(address) = lower(?1)) then (1)"
|
krista@2461
|
121 |
" when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
|
krista@2461
|
122 |
" else 0"
|
krista@2461
|
123 |
" end) = 1"
|
krista@2505
|
124 |
" and identity.user_id = ?2 "
|
krista@2506
|
125 |
" order by is_own desc, "
|
krista@2506
|
126 |
" timestamp desc; ";
|
krista@2461
|
127 |
|
krista@2461
|
128 |
static const char *sql_get_identities_by_address =
|
krista@2461
|
129 |
"select user_id, identity.main_key_id, username, lang,"
|
krista@3730
|
130 |
" identity.flags, is_own, pEp_version_major, pEp_version_minor"
|
krista@2461
|
131 |
" from identity"
|
krista@2461
|
132 |
" join person on id = identity.user_id"
|
krista@2461
|
133 |
" where (case when (address = ?1) then (1)"
|
krista@2461
|
134 |
" when (lower(address) = lower(?1)) then (1)"
|
krista@2461
|
135 |
" when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
|
krista@2461
|
136 |
" else 0"
|
krista@2505
|
137 |
" end) = 1 "
|
krista@2506
|
138 |
" order by is_own desc, "
|
krista@2506
|
139 |
" timestamp desc; ";
|
krista@2966
|
140 |
|
krista@3001
|
141 |
static const char *sql_get_identities_by_userid =
|
krista@3001
|
142 |
"select address, fpr, username, comm_type, lang,"
|
krista@3001
|
143 |
" identity.flags | pgp_keypair.flags,"
|
krista@3730
|
144 |
" is_own, pEp_version_major, pEp_version_minor"
|
krista@2966
|
145 |
" from identity"
|
krista@2966
|
146 |
" join person on id = identity.user_id"
|
krista@3001
|
147 |
" join pgp_keypair on fpr = identity.main_key_id"
|
krista@3001
|
148 |
" join trust on id = trust.user_id"
|
krista@3001
|
149 |
" and pgp_keypair_fpr = identity.main_key_id"
|
krista@3001
|
150 |
" where identity.user_id = ?1"
|
krista@2966
|
151 |
" order by is_own desc, "
|
krista@2966
|
152 |
" timestamp desc; ";
|
krista@2461
|
153 |
|
krista@1799
|
154 |
static const char *sql_replace_identities_fpr =
|
krista@1799
|
155 |
"update identity"
|
krista@1799
|
156 |
" set main_key_id = ?1 "
|
krista@1799
|
157 |
" where main_key_id = ?2 ;";
|
krista@2461
|
158 |
|
krista@2461
|
159 |
static const char *sql_remove_fpr_as_default =
|
krista@2461
|
160 |
"update person set main_key_id = NULL where main_key_id = ?1 ;"
|
krista@2461
|
161 |
"update identity set main_key_id = NULL where main_key_id = ?1 ;";
|
krista@1793
|
162 |
|
edouard@1518
|
163 |
// Set person, but if already exist, only update.
|
edouard@1518
|
164 |
// if main_key_id already set, don't touch.
|
edouard@1518
|
165 |
static const char *sql_set_person =
|
krista@3420
|
166 |
"insert into person (id, username, lang, main_key_id)"
|
krista@3420
|
167 |
" values (?1, ?2, ?3, ?4) ;";
|
krista@2478
|
168 |
|
krista@2478
|
169 |
static const char *sql_update_person =
|
krista@2477
|
170 |
"update person "
|
krista@2477
|
171 |
" set username = ?2, "
|
krista@2477
|
172 |
" lang = ?3, "
|
krista@2477
|
173 |
" main_key_id = "
|
krista@2477
|
174 |
" (select coalesce( "
|
krista@2477
|
175 |
" (select main_key_id from person where id = ?1), "
|
krista@3420
|
176 |
" upper(replace(?4,' ',''))))"
|
krista@2477
|
177 |
" where id = ?1 ;";
|
krista@2965
|
178 |
|
krista@2965
|
179 |
// Will cascade.
|
krista@2965
|
180 |
static const char *sql_delete_person =
|
krista@2965
|
181 |
"delete from person where id = ?1 ;";
|
krista@2965
|
182 |
|
vb@2833
|
183 |
static const char *sql_set_as_pEp_user =
|
vb@2833
|
184 |
"update person set is_pEp_user = 1 "
|
krista@2468
|
185 |
" where id = ?1 ; ";
|
krista@2468
|
186 |
|
vb@2833
|
187 |
static const char *sql_is_pEp_user =
|
vb@2833
|
188 |
"select is_pEp_user from person "
|
krista@2468
|
189 |
" where id = ?1 ; ";
|
krista@2468
|
190 |
|
krista@2468
|
191 |
static const char* sql_exists_person =
|
krista@2468
|
192 |
"select count(*) from person "
|
krista@2468
|
193 |
" where id = ?1 ;";
|
krista@2468
|
194 |
|
krista@2461
|
195 |
// This will cascade to identity and trust
|
krista@2461
|
196 |
static const char* sql_replace_userid =
|
krista@2461
|
197 |
"update person set id = ?1 "
|
krista@2468
|
198 |
" where id = ?2;";
|
krista@2461
|
199 |
|
krista@2948
|
200 |
// Hopefully this cascades and removes trust entries...
|
krista@2948
|
201 |
static const char *sql_delete_key =
|
krista@2948
|
202 |
"delete from pgp_keypair "
|
krista@2948
|
203 |
" where fpr = ?1 ; ";
|
krista@2948
|
204 |
|
krista@2461
|
205 |
static const char *sql_replace_main_user_fpr =
|
krista@2461
|
206 |
"update person "
|
krista@2461
|
207 |
" set main_key_id = ?1 "
|
krista@2461
|
208 |
" where id = ?2 ;";
|
krista@2461
|
209 |
|
krista@2461
|
210 |
static const char *sql_get_main_user_fpr =
|
krista@2461
|
211 |
"select main_key_id from person"
|
krista@2461
|
212 |
" where id = ?1 ;";
|
krista@2461
|
213 |
|
krista@2461
|
214 |
static const char *sql_refresh_userid_default_key =
|
krista@2461
|
215 |
"update person "
|
krista@2461
|
216 |
" set main_key_id = "
|
krista@2461
|
217 |
" (select identity.main_key_id from identity "
|
krista@2461
|
218 |
" join trust on trust.user_id = identity.user_id "
|
krista@2461
|
219 |
" and trust.pgp_keypair_fpr = identity.main_key_id "
|
krista@2461
|
220 |
" join person on identity.user_id = identity.user_id "
|
krista@2461
|
221 |
" where identity.user_id = ?1 "
|
krista@2461
|
222 |
" order by trust.comm_type desc "
|
krista@2461
|
223 |
" limit 1) "
|
krista@2461
|
224 |
"where id = ?1 ; ";
|
edouard@1518
|
225 |
|
edouard@1518
|
226 |
static const char *sql_set_pgp_keypair =
|
krista@2477
|
227 |
"insert or ignore into pgp_keypair (fpr) "
|
edouard@1518
|
228 |
"values (upper(replace(?1,' ',''))) ;";
|
edouard@1518
|
229 |
|
krista@2480
|
230 |
static const char* sql_exists_identity_entry =
|
krista@2480
|
231 |
"select count(*) from identity "
|
krista@2481
|
232 |
" where (case when (address = ?1) then (1)"
|
krista@2481
|
233 |
" when (lower(address) = lower(?1)) then (1)"
|
krista@2481
|
234 |
" when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
|
krista@2481
|
235 |
" else 0"
|
krista@2481
|
236 |
" end) = 1"
|
krista@2481
|
237 |
" and user_id = ?2;";
|
krista@2481
|
238 |
|
krista@2478
|
239 |
static const char *sql_set_identity_entry =
|
krista@2479
|
240 |
"insert into identity ("
|
krista@2477
|
241 |
" address, main_key_id, "
|
krista@3730
|
242 |
" user_id, flags, is_own,"
|
krista@3730
|
243 |
" pEp_version_major, pEp_version_minor"
|
krista@2477
|
244 |
" ) values ("
|
krista@2477
|
245 |
" ?1,"
|
krista@2477
|
246 |
" upper(replace(?2,' ','')),"
|
krista@2477
|
247 |
" ?3,"
|
krista@2477
|
248 |
" ?4,"
|
krista@3730
|
249 |
" ?5,"
|
krista@3730
|
250 |
" ?6,"
|
krista@3730
|
251 |
" ?7"
|
krista@2478
|
252 |
" );";
|
krista@2478
|
253 |
|
krista@2478
|
254 |
static const char* sql_update_identity_entry =
|
krista@2477
|
255 |
"update identity "
|
krista@2477
|
256 |
" set main_key_id = upper(replace(?2,' ','')), "
|
krista@2477
|
257 |
" flags = ?4, "
|
krista@3730
|
258 |
" is_own = ?5, "
|
krista@3730
|
259 |
" pEp_version_major = ?6, "
|
krista@3730
|
260 |
" pEp_version_major = ?7 "
|
krista@2481
|
261 |
" where (case when (address = ?1) then (1)"
|
krista@2481
|
262 |
" when (lower(address) = lower(?1)) then (1)"
|
krista@2481
|
263 |
" when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1) "
|
krista@2481
|
264 |
" else 0 "
|
krista@2481
|
265 |
" end) = 1 "
|
krista@2481
|
266 |
" and user_id = ?3 ;";
|
krista@2477
|
267 |
|
edouard@1518
|
268 |
// " (select"
|
edouard@1518
|
269 |
// " coalesce("
|
edouard@1518
|
270 |
// " (select flags from identity"
|
edouard@1518
|
271 |
// " where address = ?1 and"
|
edouard@1518
|
272 |
// " user_id = ?3),"
|
edouard@1518
|
273 |
// " 0)"
|
edouard@1518
|
274 |
// " ) | (?4 & 255)"
|
edouard@1518
|
275 |
/* set_identity ignores previous flags, and doesn't filter machine flags */
|
edouard@1518
|
276 |
|
edouard@1518
|
277 |
static const char *sql_set_identity_flags =
|
edouard@1518
|
278 |
"update identity set flags = "
|
krista@3422
|
279 |
" ((?1 & 65535) | (select flags from identity"
|
krista@2481
|
280 |
" where (case when (address = ?2) then (1)"
|
krista@2481
|
281 |
" when (lower(address) = lower(?2)) then (1)"
|
krista@2481
|
282 |
" when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
|
krista@2481
|
283 |
" else 0 "
|
krista@2481
|
284 |
" end) = 1 "
|
krista@2481
|
285 |
" and user_id = ?3)) "
|
krista@2481
|
286 |
" where (case when (address = ?2) then (1)"
|
krista@2481
|
287 |
" when (lower(address) = lower(?2)) then (1)"
|
krista@2481
|
288 |
" when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
|
krista@2481
|
289 |
" else 0"
|
krista@2481
|
290 |
" end) = 1"
|
krista@2481
|
291 |
" and user_id = ?3 ;";
|
edouard@1518
|
292 |
|
edouard@1518
|
293 |
static const char *sql_unset_identity_flags =
|
edouard@1518
|
294 |
"update identity set flags = "
|
krista@3422
|
295 |
" ( ~(?1 & 65535) & (select flags from identity"
|
krista@2481
|
296 |
" where (case when (address = ?2) then (1)"
|
krista@2481
|
297 |
" when (lower(address) = lower(?2)) then (1)"
|
krista@2481
|
298 |
" when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
|
krista@2481
|
299 |
" else 0 "
|
krista@2481
|
300 |
" end) = 1 "
|
krista@2481
|
301 |
" and user_id = ?3)) "
|
krista@2481
|
302 |
" where (case when (address = ?2) then (1)"
|
krista@2481
|
303 |
" when (lower(address) = lower(?2)) then (1)"
|
krista@2481
|
304 |
" when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
|
krista@2481
|
305 |
" else 0"
|
krista@2481
|
306 |
" end) = 1"
|
krista@2481
|
307 |
" and user_id = ?3 ;";
|
edouard@1518
|
308 |
|
krista@3729
|
309 |
static const char *sql_set_pEp_version =
|
krista@3729
|
310 |
"update identity "
|
krista@3730
|
311 |
" set pEp_version_major = ?1, "
|
krista@3730
|
312 |
" pEp_version_minor = ?2 "
|
krista@3730
|
313 |
" where (case when (address = ?3) then (1)"
|
krista@3730
|
314 |
" when (lower(address) = lower(?3)) then (1)"
|
krista@3730
|
315 |
" when (replace(lower(address),'.','') = replace(lower(?3),'.','')) then (1) "
|
krista@3729
|
316 |
" else 0 "
|
krista@3729
|
317 |
" end) = 1 "
|
krista@3730
|
318 |
" and user_id = ?4 ;";
|
krista@3730
|
319 |
|
krista@3730
|
320 |
static const char *sql_upgrade_pEp_version_by_user_id =
|
krista@3730
|
321 |
"update identity "
|
krista@3730
|
322 |
" set pEp_version_major = ?1, "
|
krista@3730
|
323 |
" pEp_version_minor = ?2 "
|
krista@3730
|
324 |
" where user_id = ?3 "
|
krista@3730
|
325 |
" and (case when (pEp_version_major < ?1) then (1)"
|
krista@3730
|
326 |
" when (pEp_version_major > ?1) then (0)"
|
krista@3730
|
327 |
" when (pEp_version_minor < ?2) then (1)"
|
krista@3730
|
328 |
" else 0 "
|
krista@3730
|
329 |
" end) = 1 ;";
|
krista@3729
|
330 |
|
edouard@1518
|
331 |
static const char *sql_set_trust =
|
krista@2479
|
332 |
"insert into trust (user_id, pgp_keypair_fpr, comm_type) "
|
krista@2478
|
333 |
"values (?1, upper(replace(?2,' ','')), ?3) ;";
|
krista@2478
|
334 |
|
krista@2478
|
335 |
static const char *sql_update_trust =
|
krista@2477
|
336 |
"update trust set comm_type = ?3 "
|
krista@2477
|
337 |
" where user_id = ?1 and pgp_keypair_fpr = upper(replace(?2,' ',''));";
|
krista@2480
|
338 |
|
vb@2834
|
339 |
static const char *sql_update_trust_to_pEp =
|
krista@2543
|
340 |
"update trust set comm_type = comm_type + 71 "
|
krista@2543
|
341 |
" where (user_id = ?1 "
|
krista@2543
|
342 |
" and (case when (comm_type = 56) then (1) "
|
krista@2543
|
343 |
" when (comm_type = 184) then (1) "
|
krista@2543
|
344 |
" else 0"
|
krista@2543
|
345 |
" end) = 1); ";
|
krista@2543
|
346 |
|
krista@2480
|
347 |
static const char* sql_exists_trust_entry =
|
krista@2480
|
348 |
"select count(*) from trust "
|
krista@2480
|
349 |
" where user_id = ?1 and pgp_keypair_fpr = upper(replace(?2,' ',''));";
|
krista@2477
|
350 |
|
krista@1799
|
351 |
static const char *sql_update_trust_for_fpr =
|
krista@1799
|
352 |
"update trust "
|
krista@1799
|
353 |
"set comm_type = ?1 "
|
krista@1799
|
354 |
"where pgp_keypair_fpr = upper(replace(?2,' ','')) ;";
|
krista@1799
|
355 |
|
edouard@1518
|
356 |
static const char *sql_get_trust =
|
edouard@1518
|
357 |
"select comm_type from trust where user_id = ?1 "
|
edouard@1518
|
358 |
"and pgp_keypair_fpr = upper(replace(?2,' ','')) ;";
|
edouard@1518
|
359 |
|
krista@2967
|
360 |
static const char *sql_get_trust_by_userid =
|
krista@2967
|
361 |
"select pgp_keypair_fpr, comm_type from trust where user_id = ?1 ";
|
krista@2967
|
362 |
|
edouard@1518
|
363 |
static const char *sql_least_trust =
|
edouard@1632
|
364 |
"select min(comm_type) from trust where"
|
edouard@1632
|
365 |
" pgp_keypair_fpr = upper(replace(?1,' ',''))"
|
edouard@1632
|
366 |
" and comm_type != 0;"; // ignores PEP_ct_unknown
|
edouard@1632
|
367 |
// returns PEP_ct_unknown only when no known trust is recorded
|
edouard@1518
|
368 |
|
krista@2593
|
369 |
static const char *sql_mark_as_compromised =
|
edouard@1518
|
370 |
"update trust not indexed set comm_type = 15"
|
edouard@1518
|
371 |
" where pgp_keypair_fpr = upper(replace(?1,' ','')) ;";
|
krista@2461
|
372 |
|
edouard@1518
|
373 |
static const char *sql_crashdump =
|
edouard@1518
|
374 |
"select timestamp, title, entity, description, comment"
|
edouard@1518
|
375 |
" from log order by timestamp desc limit ?1 ;";
|
edouard@1518
|
376 |
|
edouard@1518
|
377 |
static const char *sql_languagelist =
|
edouard@1518
|
378 |
"select i18n_language.lang, name, phrase"
|
edouard@1518
|
379 |
" from i18n_language join i18n_token using (lang) where i18n_token.id = 1000;" ;
|
edouard@1518
|
380 |
|
edouard@1518
|
381 |
static const char *sql_i18n_token =
|
edouard@1518
|
382 |
"select phrase from i18n_token where lang = lower(?1) and id = ?2 ;";
|
krista@3729
|
383 |
|
edouard@1518
|
384 |
// blacklist
|
edouard@1518
|
385 |
static const char *sql_blacklist_add =
|
krista@2477
|
386 |
"insert or ignore into blacklist_keys (fpr) values (upper(replace(?1,' ',''))) ;"
|
edouard@1518
|
387 |
"delete from identity where main_key_id = upper(replace(?1,' ','')) ;"
|
edouard@1518
|
388 |
"delete from pgp_keypair where fpr = upper(replace(?1,' ','')) ;";
|
edouard@1518
|
389 |
|
edouard@1518
|
390 |
static const char *sql_blacklist_delete =
|
edouard@1518
|
391 |
"delete from blacklist_keys where fpr = upper(replace(?1,' ','')) ;";
|
edouard@1518
|
392 |
|
edouard@1518
|
393 |
static const char *sql_blacklist_is_listed =
|
edouard@1518
|
394 |
"select count(*) from blacklist_keys where fpr = upper(replace(?1,' ','')) ;";
|
edouard@1518
|
395 |
|
edouard@1518
|
396 |
static const char *sql_blacklist_retrieve =
|
edouard@1518
|
397 |
"select * from blacklist_keys ;";
|
edouard@1518
|
398 |
|
edouard@1518
|
399 |
|
edouard@1518
|
400 |
// Own keys
|
krista@2461
|
401 |
// We only care if it's 0 or non-zero
|
edouard@1518
|
402 |
static const char *sql_own_key_is_listed =
|
edouard@1518
|
403 |
"select count(*) from ("
|
krista@2461
|
404 |
" select pgp_keypair_fpr from trust"
|
krista@2461
|
405 |
" join identity on trust.user_id = identity.user_id"
|
krista@2461
|
406 |
" where pgp_keypair_fpr = upper(replace(?1,' ',''))"
|
krista@2461
|
407 |
" and identity.is_own = 1"
|
krista@2461
|
408 |
");";
|
krista@3346
|
409 |
|
krista@3346
|
410 |
static const char *sql_is_own_address =
|
krista@3346
|
411 |
"select count(*) from ("
|
krista@3346
|
412 |
" select address from identity"
|
krista@3346
|
413 |
" where (case when (address = ?1) then (1)"
|
krista@3346
|
414 |
" when (lower(address) = lower(?1)) then (1)"
|
krista@3346
|
415 |
" when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
|
krista@3346
|
416 |
" else 0"
|
krista@3346
|
417 |
" end) = 1 "
|
krista@3346
|
418 |
" and identity.is_own = 1"
|
krista@3346
|
419 |
");";
|
edouard@1518
|
420 |
|
edouard@1518
|
421 |
static const char *sql_own_identities_retrieve =
|
vb@3100
|
422 |
"select address, fpr, identity.user_id, username,"
|
krista@3730
|
423 |
" lang, identity.flags | pgp_keypair.flags, pEp_version_major, pEp_version_minor"
|
edouard@1518
|
424 |
" from identity"
|
edouard@1518
|
425 |
" join person on id = identity.user_id"
|
edouard@1518
|
426 |
" join pgp_keypair on fpr = identity.main_key_id"
|
edouard@1518
|
427 |
" join trust on id = trust.user_id"
|
edouard@1518
|
428 |
" and pgp_keypair_fpr = identity.main_key_id"
|
krista@2461
|
429 |
" where identity.is_own = 1"
|
edouard@1518
|
430 |
" and (identity.flags & ?1) = 0;";
|
krista@2461
|
431 |
|
krista@2461
|
432 |
static const char *sql_own_keys_retrieve =
|
krista@2461
|
433 |
"select pgp_keypair_fpr from trust"
|
krista@2461
|
434 |
" join identity on trust.user_id = identity.user_id"
|
krista@2461
|
435 |
" where identity.is_own = 1";
|
krista@2461
|
436 |
|
krista@2461
|
437 |
static const char* sql_get_user_default_key =
|
krista@2461
|
438 |
"select main_key_id from person"
|
krista@2461
|
439 |
" where id = ?1;";
|
krista@2461
|
440 |
|
krista@2893
|
441 |
static const char* sql_get_all_keys_for_user =
|
krista@2893
|
442 |
"select pgp_keypair_fpr from trust"
|
krista@2893
|
443 |
" where user_id = ?1; ";
|
krista@2893
|
444 |
|
krista@2461
|
445 |
static const char* sql_get_default_own_userid =
|
krista@2461
|
446 |
"select id from person"
|
krista@2461
|
447 |
" join identity on id = identity.user_id"
|
krista@2461
|
448 |
" where identity.is_own = 1";
|
edouard@1518
|
449 |
|
edouard@1518
|
450 |
// Sequence
|
edouard@1518
|
451 |
static const char *sql_sequence_value1 =
|
vb@2830
|
452 |
"insert or replace into sequences (name, value) "
|
edouard@1518
|
453 |
"values (?1, "
|
edouard@1602
|
454 |
" (select coalesce((select value + 1 from sequences "
|
vb@2830
|
455 |
" where name = ?1), 1 ))); ";
|
edouard@1518
|
456 |
|
edouard@1518
|
457 |
static const char *sql_sequence_value2 =
|
vb@2848
|
458 |
"select value from sequences where name = ?1 ;";
|
edouard@1518
|
459 |
|
edouard@1518
|
460 |
// Revocation tracking
|
edouard@1518
|
461 |
static const char *sql_set_revoked =
|
edouard@1518
|
462 |
"insert or replace into revoked_keys ("
|
edouard@1518
|
463 |
" revoked_fpr, replacement_fpr, revocation_date) "
|
edouard@1518
|
464 |
"values (upper(replace(?1,' ','')),"
|
edouard@1518
|
465 |
" upper(replace(?2,' ','')),"
|
edouard@1518
|
466 |
" ?3) ;";
|
edouard@1518
|
467 |
|
edouard@1518
|
468 |
static const char *sql_get_revoked =
|
edouard@1518
|
469 |
"select revoked_fpr, revocation_date from revoked_keys"
|
edouard@1518
|
470 |
" where replacement_fpr = upper(replace(?1,' ','')) ;";
|
edouard@1518
|
471 |
|
krista@2752
|
472 |
static const char *sql_get_replacement_fpr =
|
krista@2752
|
473 |
"select replacement_fpr, revocation_date from revoked_keys"
|
krista@2752
|
474 |
" where revoked_fpr = upper(replace(?1,' ','')) ;";
|
krista@2752
|
475 |
|
krista@2461
|
476 |
static const char *sql_get_userid_alias_default =
|
krista@2461
|
477 |
"select default_id from alternate_user_id "
|
krista@2461
|
478 |
" where alternate_id = ?1 ; ";
|
krista@2461
|
479 |
|
krista@2471
|
480 |
// Revocation tracking
|
krista@2471
|
481 |
static const char *sql_add_mistrusted_key =
|
krista@2478
|
482 |
"insert or replace into mistrusted_keys (fpr) "
|
krista@2471
|
483 |
" values (upper(replace(?1,' ',''))) ;";
|
krista@2471
|
484 |
|
krista@2471
|
485 |
static const char *sql_delete_mistrusted_key =
|
krista@2586
|
486 |
"delete from mistrusted_keys where fpr = upper(replace(?1,' ','')) ;";
|
krista@2471
|
487 |
|
krista@2471
|
488 |
static const char *sql_is_mistrusted_key =
|
krista@2471
|
489 |
"select count(*) from mistrusted_keys where fpr = upper(replace(?1,' ','')) ;";
|
krista@2471
|
490 |
|
krista@2461
|
491 |
static const char *sql_add_userid_alias =
|
krista@2479
|
492 |
"insert or replace into alternate_user_id (alternate_id, default_id) "
|
krista@2479
|
493 |
"values (?2, ?1) ;";
|
krista@2747
|
494 |
|
krista@2747
|
495 |
static const char *sql_add_into_social_graph =
|
krista@2752
|
496 |
"insert or replace into social_graph(own_userid, own_address, contact_userid) "
|
krista@2747
|
497 |
"values (?1, ?2, ?3) ;";
|
krista@2752
|
498 |
|
krista@2752
|
499 |
static const char *sql_get_own_address_binding_from_contact =
|
krista@2752
|
500 |
"select own_address from social_graph where own_userid = ?1 and contact_userid = ?2 ;";
|
krista@2752
|
501 |
|
krista@2752
|
502 |
static const char *sql_set_revoke_contact_as_notified =
|
krista@2752
|
503 |
"insert or replace into revocation_contact_list(fpr, contact_id) values (?1, ?2) ;";
|
krista@2752
|
504 |
|
krista@2752
|
505 |
static const char *sql_get_contacted_ids_from_revoke_fpr =
|
krista@2752
|
506 |
"select * from revocation_contact_list where fpr = ?1 ;";
|
krista@2752
|
507 |
|
krista@2752
|
508 |
static const char *sql_was_id_for_revoke_contacted =
|
krista@2752
|
509 |
"select count(*) from revocation_contact_list where fpr = ?1 and contact_id = ?2 ;";
|
krista@2800
|
510 |
|
krista@2800
|
511 |
// We only need user_id and address, since in the main usage, we'll call update_identity
|
krista@2800
|
512 |
// on this anyway when sending out messages.
|
krista@2800
|
513 |
static const char *sql_get_last_contacted =
|
krista@2862
|
514 |
"select user_id, address from identity where datetime('now') < datetime(timestamp, '+14 days') ; ";
|
krista@2461
|
515 |
|
vb@928
|
516 |
static int user_version(void *_version, int count, char **text, char **name)
|
vb@928
|
517 |
{
|
vb@928
|
518 |
assert(_version);
|
vb@928
|
519 |
assert(count == 1);
|
vb@928
|
520 |
assert(text && text[0]);
|
vb@928
|
521 |
if (!(_version && count == 1 && text && text[0]))
|
vb@928
|
522 |
return -1;
|
vb@928
|
523 |
|
vb@928
|
524 |
int *version = (int *) _version;
|
vb@928
|
525 |
*version = atoi(text[0]);
|
vb@928
|
526 |
return 0;
|
vb@928
|
527 |
}
|
vb@928
|
528 |
|
krista@3331
|
529 |
// TODO: refactor and generalise these two functions if possible.
|
krista@3331
|
530 |
static int db_contains_table(PEP_SESSION session, const char* table_name) {
|
krista@3331
|
531 |
if (!session || !table_name)
|
krista@3331
|
532 |
return -1;
|
krista@3331
|
533 |
|
krista@3331
|
534 |
// Table names can't be SQL parameters, so we do it this way.
|
krista@3331
|
535 |
|
krista@3331
|
536 |
// these two must be the same number.
|
krista@3331
|
537 |
char sql_buf[500];
|
krista@3331
|
538 |
const size_t max_q_len = 500;
|
krista@3331
|
539 |
|
krista@3331
|
540 |
size_t t_size, q_size;
|
krista@3331
|
541 |
|
krista@3331
|
542 |
const char* q1 = "SELECT name FROM sqlite_master WHERE type='table' AND name='{"; // 61
|
krista@3460
|
543 |
const char* q2 = "}';"; // 3
|
krista@3331
|
544 |
|
krista@3331
|
545 |
q_size = 64;
|
krista@3331
|
546 |
t_size = strlen(table_name);
|
krista@3331
|
547 |
|
krista@3331
|
548 |
size_t query_len = q_size + t_size + 1;
|
krista@3331
|
549 |
|
krista@3331
|
550 |
if (query_len > max_q_len)
|
krista@3331
|
551 |
return -1;
|
krista@3331
|
552 |
|
krista@3331
|
553 |
strlcpy(sql_buf, q1, max_q_len);
|
krista@3331
|
554 |
strlcat(sql_buf, table_name, max_q_len);
|
krista@3331
|
555 |
strlcat(sql_buf, q2, max_q_len);
|
krista@3331
|
556 |
|
krista@3331
|
557 |
sqlite3_stmt *stmt;
|
krista@3331
|
558 |
|
krista@3331
|
559 |
sqlite3_prepare_v2(session->db, sql_buf, -1, &stmt, NULL);
|
krista@3331
|
560 |
|
krista@3331
|
561 |
int retval = 0;
|
krista@3331
|
562 |
|
krista@3331
|
563 |
int rc = sqlite3_step(stmt);
|
krista@3331
|
564 |
if (rc == SQLITE_DONE || rc == SQLITE_OK || rc == SQLITE_ROW) {
|
krista@3331
|
565 |
retval = 1;
|
krista@3331
|
566 |
}
|
krista@3331
|
567 |
|
krista@3331
|
568 |
sqlite3_finalize(stmt);
|
krista@3331
|
569 |
|
krista@3331
|
570 |
return retval;
|
krista@3331
|
571 |
|
krista@3331
|
572 |
}
|
krista@3331
|
573 |
|
krista@2461
|
574 |
static int table_contains_column(PEP_SESSION session, const char* table_name,
|
krista@2461
|
575 |
const char* col_name) {
|
krista@2461
|
576 |
|
krista@2461
|
577 |
|
krista@2461
|
578 |
if (!session || !table_name || !col_name)
|
krista@2461
|
579 |
return -1;
|
krista@2461
|
580 |
|
krista@2461
|
581 |
// Table names can't be SQL parameters, so we do it this way.
|
krista@2461
|
582 |
|
krista@2461
|
583 |
// these two must be the same number.
|
krista@2461
|
584 |
char sql_buf[500];
|
krista@2461
|
585 |
const size_t max_q_len = 500;
|
krista@2461
|
586 |
|
krista@2461
|
587 |
size_t t_size, c_size, q_size;
|
krista@2461
|
588 |
|
krista@2461
|
589 |
const char* q1 = "SELECT "; // 7
|
krista@2461
|
590 |
const char* q2 = " from "; // 6
|
krista@2461
|
591 |
const char* q3 = ";"; // 1
|
krista@2461
|
592 |
|
krista@2461
|
593 |
q_size = 14;
|
krista@2461
|
594 |
t_size = strlen(table_name);
|
krista@2461
|
595 |
c_size = strlen(col_name);
|
krista@2461
|
596 |
|
krista@2461
|
597 |
size_t query_len = q_size + c_size + t_size + 1;
|
krista@2461
|
598 |
|
krista@2461
|
599 |
if (query_len > max_q_len)
|
krista@2461
|
600 |
return -1;
|
krista@2461
|
601 |
|
krista@2461
|
602 |
strlcpy(sql_buf, q1, max_q_len);
|
krista@2461
|
603 |
strlcat(sql_buf, col_name, max_q_len);
|
krista@2461
|
604 |
strlcat(sql_buf, q2, max_q_len);
|
krista@2461
|
605 |
strlcat(sql_buf, table_name, max_q_len);
|
krista@2461
|
606 |
strlcat(sql_buf, q3, max_q_len);
|
krista@2461
|
607 |
|
krista@2461
|
608 |
sqlite3_stmt *stmt;
|
krista@2461
|
609 |
|
krista@2461
|
610 |
sqlite3_prepare_v2(session->db, sql_buf, -1, &stmt, NULL);
|
krista@2461
|
611 |
|
krista@2461
|
612 |
int retval = 0;
|
krista@2461
|
613 |
|
krista@2461
|
614 |
int rc = sqlite3_step(stmt);
|
krista@2461
|
615 |
if (rc == SQLITE_DONE || rc == SQLITE_OK || rc == SQLITE_ROW) {
|
krista@2461
|
616 |
retval = 1;
|
krista@2461
|
617 |
}
|
krista@2461
|
618 |
|
krista@2461
|
619 |
sqlite3_finalize(stmt);
|
krista@2461
|
620 |
|
krista@2461
|
621 |
return retval;
|
krista@2461
|
622 |
}
|
krista@2461
|
623 |
|
krista@3463
|
624 |
PEP_STATUS repair_altered_tables(PEP_SESSION session) {
|
krista@3463
|
625 |
PEP_STATUS status = PEP_STATUS_OK;
|
krista@3463
|
626 |
|
krista@3463
|
627 |
const unsigned int _PEP_MAX_AFFECTED = 5;
|
krista@3463
|
628 |
char** table_names = calloc(_PEP_MAX_AFFECTED, sizeof(char*));
|
krista@3463
|
629 |
if (!table_names)
|
krista@3463
|
630 |
return PEP_OUT_OF_MEMORY;
|
krista@3463
|
631 |
|
krista@3463
|
632 |
const char* sql_query = "select tbl_name from sqlite_master WHERE sql LIKE '%REFERENCES%' AND sql LIKE '%_old%';";
|
krista@3463
|
633 |
sqlite3_stmt *stmt;
|
krista@3463
|
634 |
sqlite3_prepare_v2(session->db, sql_query, -1, &stmt, NULL);
|
krista@3463
|
635 |
int i = 0;
|
krista@3463
|
636 |
int int_result = 0;
|
krista@3463
|
637 |
while ((int_result = sqlite3_step(stmt)) == SQLITE_ROW && i < _PEP_MAX_AFFECTED) {
|
krista@3463
|
638 |
table_names[i++] = strdup((const char*)(sqlite3_column_text(stmt, 0)));
|
krista@3463
|
639 |
}
|
krista@3463
|
640 |
|
krista@3463
|
641 |
sqlite3_finalize(stmt);
|
krista@3463
|
642 |
|
krista@3463
|
643 |
if ((int_result != SQLITE_DONE && int_result != SQLITE_OK) || i > (_PEP_MAX_AFFECTED + 1)) {
|
krista@3463
|
644 |
status = PEP_UNKNOWN_DB_ERROR;
|
krista@3463
|
645 |
goto pEp_free;
|
krista@3463
|
646 |
}
|
krista@3463
|
647 |
|
krista@3463
|
648 |
for (i = 0; i < _PEP_MAX_AFFECTED; i++) {
|
krista@3463
|
649 |
const char* table_name = table_names[i];
|
krista@3463
|
650 |
if (!table_name)
|
krista@3463
|
651 |
break;
|
krista@3463
|
652 |
|
krista@3463
|
653 |
if (strcmp(table_name, "identity") == 0) {
|
krista@3463
|
654 |
int_result = sqlite3_exec(session->db,
|
krista@3463
|
655 |
"PRAGMA foreign_keys=off;\n"
|
krista@3463
|
656 |
"BEGIN TRANSACTION;\n"
|
krista@3463
|
657 |
"create table _identity_new (\n"
|
krista@3463
|
658 |
" address text,\n"
|
krista@3463
|
659 |
" user_id text\n"
|
krista@3463
|
660 |
" references person (id)\n"
|
krista@3463
|
661 |
" on delete cascade on update cascade,\n"
|
krista@3463
|
662 |
" main_key_id text\n"
|
krista@3463
|
663 |
" references pgp_keypair (fpr)\n"
|
krista@3463
|
664 |
" on delete set null,\n"
|
krista@3463
|
665 |
" comment text,\n"
|
krista@3463
|
666 |
" flags integer default 0,\n"
|
krista@3463
|
667 |
" is_own integer default 0,\n"
|
krista@3463
|
668 |
" timestamp integer default (datetime('now')),\n"
|
krista@3463
|
669 |
" primary key (address, user_id)\n"
|
krista@3463
|
670 |
");\n"
|
krista@3463
|
671 |
"INSERT INTO _identity_new SELECT * from identity;\n"
|
krista@3463
|
672 |
"DROP TABLE identity;\n"
|
krista@3463
|
673 |
"ALTER TABLE _identity_new RENAME TO identity;\n"
|
krista@3463
|
674 |
"COMMIT;\n"
|
krista@3463
|
675 |
"PRAGMA foreign_keys=on;"
|
krista@3463
|
676 |
,
|
krista@3463
|
677 |
NULL,
|
krista@3463
|
678 |
NULL,
|
krista@3463
|
679 |
NULL
|
krista@3463
|
680 |
);
|
krista@3463
|
681 |
assert(int_result == PEP_STATUS_OK);
|
krista@3463
|
682 |
}
|
krista@3463
|
683 |
else if (strcmp(table_name, "trust") == 0) {
|
krista@3463
|
684 |
int_result = sqlite3_exec(session->db,
|
krista@3463
|
685 |
"PRAGMA foreign_keys=off;\n"
|
krista@3463
|
686 |
"BEGIN TRANSACTION;\n"
|
krista@3463
|
687 |
"create table _trust_new (\n"
|
krista@3463
|
688 |
" user_id text not null\n"
|
krista@3463
|
689 |
" references person (id)\n"
|
krista@3463
|
690 |
" on delete cascade on update cascade,\n"
|
krista@3463
|
691 |
" pgp_keypair_fpr text not null\n"
|
krista@3463
|
692 |
" references pgp_keypair (fpr)\n"
|
krista@3463
|
693 |
" on delete cascade,\n"
|
krista@3463
|
694 |
" comm_type integer not null,\n"
|
krista@3463
|
695 |
" comment text,\n"
|
krista@3463
|
696 |
" primary key (user_id, pgp_keypair_fpr)\n"
|
krista@3463
|
697 |
");\n"
|
krista@3463
|
698 |
"INSERT INTO _trust_new SELECT * from trust;\n"
|
krista@3463
|
699 |
"DROP TABLE trust;\n"
|
krista@3463
|
700 |
"ALTER TABLE _trust_new RENAME TO trust;\n"
|
krista@3463
|
701 |
"COMMIT;\n"
|
krista@3463
|
702 |
"PRAGMA foreign_keys=on;"
|
krista@3463
|
703 |
,
|
krista@3463
|
704 |
NULL,
|
krista@3463
|
705 |
NULL,
|
krista@3463
|
706 |
NULL
|
krista@3463
|
707 |
);
|
krista@3463
|
708 |
assert(int_result == PEP_STATUS_OK);
|
krista@3463
|
709 |
}
|
krista@3463
|
710 |
else if (strcmp(table_name, "alternate_user_id") == 0) {
|
krista@3463
|
711 |
int_result = sqlite3_exec(session->db,
|
krista@3463
|
712 |
"PRAGMA foreign_keys=off;\n"
|
krista@3463
|
713 |
"BEGIN TRANSACTION;\n"
|
krista@3463
|
714 |
"create table _alternate_user_id_new (\n"
|
krista@3463
|
715 |
" default_id text references person (id)\n"
|
krista@3463
|
716 |
" on delete cascade on update cascade,\n"
|
krista@3463
|
717 |
" alternate_id text primary key\n"
|
krista@3463
|
718 |
");\n"
|
krista@3463
|
719 |
"INSERT INTO _alternate_user_id_new SELECT * from alternate_user_id;\n"
|
krista@3463
|
720 |
"DROP TABLE alternate_user_id;\n"
|
krista@3463
|
721 |
"ALTER TABLE _alternate_user_id_new RENAME TO alternate_user_id;\n"
|
krista@3463
|
722 |
"COMMIT;\n"
|
krista@3463
|
723 |
"PRAGMA foreign_keys=on;"
|
krista@3463
|
724 |
,
|
krista@3463
|
725 |
NULL,
|
krista@3463
|
726 |
NULL,
|
krista@3463
|
727 |
NULL
|
krista@3463
|
728 |
);
|
krista@3463
|
729 |
assert(int_result == PEP_STATUS_OK);
|
krista@3463
|
730 |
}
|
krista@3463
|
731 |
else if (strcmp(table_name, "revocation_contact_list") == 0) {
|
krista@3463
|
732 |
int_result = sqlite3_exec(session->db,
|
krista@3463
|
733 |
"PRAGMA foreign_keys=off;\n"
|
krista@3463
|
734 |
"BEGIN TRANSACTION;\n"
|
krista@3463
|
735 |
"create table _revocation_contact_list_new (\n"
|
krista@3463
|
736 |
" fpr text not null references pgp_keypair (fpr)\n"
|
krista@3463
|
737 |
" on delete cascade,\n"
|
krista@3463
|
738 |
" contact_id text not null references person (id)\n"
|
krista@3463
|
739 |
" on delete cascade on update cascade,\n"
|
krista@3463
|
740 |
" timestamp integer default (datetime('now')),\n"
|
krista@3463
|
741 |
" PRIMARY KEY(fpr, contact_id)\n"
|
krista@3463
|
742 |
");\n"
|
krista@3463
|
743 |
"INSERT INTO _revocation_contact_list_new SELECT * from revocation_contact_list;\n"
|
krista@3463
|
744 |
"DROP TABLE revocation_contact_list;\n"
|
krista@3463
|
745 |
"ALTER TABLE _revocation_contact_list_new RENAME TO revocation_contact_list;\n"
|
krista@3463
|
746 |
"COMMIT;\n"
|
krista@3463
|
747 |
"PRAGMA foreign_keys=on;"
|
krista@3463
|
748 |
,
|
krista@3463
|
749 |
NULL,
|
krista@3463
|
750 |
NULL,
|
krista@3463
|
751 |
NULL
|
krista@3463
|
752 |
);
|
krista@3463
|
753 |
assert(int_result == PEP_STATUS_OK);
|
krista@3463
|
754 |
}
|
krista@3463
|
755 |
else if (strcmp(table_name, "social_graph")) {
|
krista@3463
|
756 |
int_result = sqlite3_exec(session->db,
|
krista@3463
|
757 |
"PRAGMA foreign_keys=off;\n"
|
krista@3463
|
758 |
"BEGIN TRANSACTION;\n"
|
krista@3463
|
759 |
"create table _social_new (\n"
|
krista@3463
|
760 |
" own_userid text,\n"
|
krista@3463
|
761 |
" own_address text,\n"
|
krista@3463
|
762 |
" contact_userid text,\n"
|
krista@3463
|
763 |
" CONSTRAINT fk_own_identity\n"
|
krista@3463
|
764 |
" FOREIGN KEY(own_address, own_userid)\n"
|
krista@3463
|
765 |
" REFERENCES identity(address, user_id)\n"
|
krista@3463
|
766 |
" ON DELETE CASCADE ON UPDATE CASCADE\n"
|
krista@3463
|
767 |
");\n"
|
krista@3463
|
768 |
"INSERT INTO _social_graph_new SELECT * from social_graph;\n"
|
krista@3463
|
769 |
"DROP TABLE social_graph;\n"
|
krista@3463
|
770 |
"ALTER TABLE _social_graph_new RENAME TO social_graph;\n"
|
krista@3463
|
771 |
"COMMIT;\n"
|
krista@3463
|
772 |
"PRAGMA foreign_keys=on;"
|
krista@3463
|
773 |
,
|
krista@3463
|
774 |
NULL,
|
krista@3463
|
775 |
NULL,
|
krista@3463
|
776 |
NULL
|
krista@3463
|
777 |
);
|
krista@3463
|
778 |
assert(int_result == PEP_STATUS_OK);
|
krista@3463
|
779 |
}
|
krista@3463
|
780 |
}
|
krista@3463
|
781 |
|
krista@3463
|
782 |
int_result = sqlite3_exec(
|
krista@3463
|
783 |
session->db,
|
krista@3463
|
784 |
"PRAGMA foreign_key_check;\n"
|
krista@3463
|
785 |
,
|
krista@3463
|
786 |
NULL,
|
krista@3463
|
787 |
NULL,
|
krista@3463
|
788 |
NULL
|
krista@3463
|
789 |
);
|
krista@3463
|
790 |
assert(int_result == SQLITE_OK);
|
krista@3463
|
791 |
|
krista@3463
|
792 |
pEp_free:
|
krista@3463
|
793 |
for (i = 0; i < _PEP_MAX_AFFECTED; i++) {
|
krista@3463
|
794 |
if (table_names[i])
|
krista@3463
|
795 |
free(table_names[i]);
|
krista@3463
|
796 |
else
|
krista@3463
|
797 |
break;
|
krista@3463
|
798 |
}
|
krista@3463
|
799 |
free(table_names);
|
krista@3463
|
800 |
return status;
|
krista@3463
|
801 |
}
|
krista@2477
|
802 |
void errorLogCallback(void *pArg, int iErrCode, const char *zMsg){
|
krista@2477
|
803 |
fprintf(stderr, "(%d) %s\n", iErrCode, zMsg);
|
krista@2477
|
804 |
}
|
krista@2477
|
805 |
|
vb@2539
|
806 |
#ifdef USE_GPG
|
vb@2539
|
807 |
PEP_STATUS pgp_import_ultimately_trusted_keypairs(PEP_SESSION session);
|
vb@2539
|
808 |
#endif // USE_GPG
|
vb@2539
|
809 |
|
vb@2833
|
810 |
DYNAMIC_API PEP_STATUS init(
|
vb@2833
|
811 |
PEP_SESSION *session,
|
vb@2879
|
812 |
messageToSend_t messageToSend,
|
vb@2879
|
813 |
inject_sync_event_t inject_sync_event
|
vb@2833
|
814 |
)
|
vb@0
|
815 |
{
|
vb@65
|
816 |
PEP_STATUS status = PEP_STATUS_OK;
|
roker@529
|
817 |
int int_result;
|
Edouard@693
|
818 |
|
vb@62
|
819 |
bool in_first = false;
|
edouard@1752
|
820 |
bool very_first = false;
|
vb@8
|
821 |
|
vb@62
|
822 |
assert(sqlite3_threadsafe());
|
vb@62
|
823 |
if (!sqlite3_threadsafe())
|
vb@62
|
824 |
return PEP_INIT_SQLITE3_WITHOUT_MUTEX;
|
vb@62
|
825 |
|
vb@62
|
826 |
// a little race condition - but still a race condition
|
vb@113
|
827 |
// mitigated by calling caveat (see documentation)
|
vb@62
|
828 |
|
krista@2125
|
829 |
// this increment is made atomic IN THE ADAPTERS by
|
krista@2125
|
830 |
// guarding the call to init with the appropriate mutex.
|
krista@2124
|
831 |
int _count = ++init_count;
|
edouard@2112
|
832 |
if (_count == 0)
|
vb@62
|
833 |
in_first = true;
|
edouard@2112
|
834 |
|
krista@2176
|
835 |
// Race condition mitigated by calling caveat starts here :
|
edouard@2112
|
836 |
// If another call to init() preempts right now, then preemptive call
|
edouard@2112
|
837 |
// will have in_first false, will not create SQL tables, and following
|
krista@2125
|
838 |
// calls relying on those tables will fail.
|
krista@2125
|
839 |
//
|
krista@2125
|
840 |
// Therefore, as above, adapters MUST guard init() with a mutex.
|
krista@2125
|
841 |
//
|
krista@2125
|
842 |
// Therefore, first session
|
edouard@2112
|
843 |
// is to be created and last session to be deleted alone, and not
|
edouard@2112
|
844 |
// concurently to other sessions creation or deletion.
|
edouard@2112
|
845 |
// We expect adapters to enforce this either by implicitely creating a
|
edouard@2112
|
846 |
// client session, or by using synchronization primitive to protect
|
edouard@2112
|
847 |
// creation/deletion of first/last session from the app.
|
vb@0
|
848 |
|
roker@529
|
849 |
assert(session);
|
vb@191
|
850 |
if (session == NULL)
|
vb@191
|
851 |
return PEP_ILLEGAL_VALUE;
|
vb@191
|
852 |
|
roker@529
|
853 |
*session = NULL;
|
vb@0
|
854 |
|
vb@107
|
855 |
pEpSession *_session = calloc(1, sizeof(pEpSession));
|
roker@529
|
856 |
assert(_session);
|
roker@529
|
857 |
if (_session == NULL)
|
roker@529
|
858 |
goto enomem;
|
vb@62
|
859 |
|
roker@529
|
860 |
_session->version = PEP_ENGINE_VERSION;
|
vb@2833
|
861 |
_session->messageToSend = messageToSend;
|
vb@2879
|
862 |
_session->inject_sync_event = inject_sync_event;
|
vb@0
|
863 |
|
roker@1722
|
864 |
#ifdef DEBUG_ERRORSTACK
|
roker@1801
|
865 |
_session->errorstack = new_stringlist("init()");
|
roker@1722
|
866 |
#endif
|
roker@1722
|
867 |
|
vb@0
|
868 |
assert(LOCAL_DB);
|
vb@0
|
869 |
if (LOCAL_DB == NULL) {
|
vb@65
|
870 |
status = PEP_INIT_CANNOT_OPEN_DB;
|
vb@2834
|
871 |
goto pEp_error;
|
vb@0
|
872 |
}
|
krista@2477
|
873 |
|
krista@2480
|
874 |
#if _PEP_SQLITE_DEBUG
|
krista@2477
|
875 |
sqlite3_config(SQLITE_CONFIG_LOG, errorLogCallback, NULL);
|
krista@2480
|
876 |
#endif
|
vb@0
|
877 |
|
roker@529
|
878 |
int_result = sqlite3_open_v2(
|
roker@529
|
879 |
LOCAL_DB,
|
roker@529
|
880 |
&_session->db,
|
roker@529
|
881 |
SQLITE_OPEN_READWRITE
|
roker@529
|
882 |
| SQLITE_OPEN_CREATE
|
roker@529
|
883 |
| SQLITE_OPEN_FULLMUTEX
|
roker@529
|
884 |
| SQLITE_OPEN_PRIVATECACHE,
|
roker@529
|
885 |
NULL
|
roker@529
|
886 |
);
|
vb@0
|
887 |
|
roker@529
|
888 |
if (int_result != SQLITE_OK) {
|
roker@529
|
889 |
status = PEP_INIT_CANNOT_OPEN_DB;
|
vb@2834
|
890 |
goto pEp_error;
|
roker@529
|
891 |
}
|
vb@0
|
892 |
|
krista@2164
|
893 |
int_result = sqlite3_exec(
|
krista@2164
|
894 |
_session->db,
|
krista@2174
|
895 |
"PRAGMA locking_mode=NORMAL;\n"
|
krista@2164
|
896 |
"PRAGMA journal_mode=WAL;\n",
|
krista@2164
|
897 |
NULL,
|
krista@2164
|
898 |
NULL,
|
krista@2164
|
899 |
NULL
|
krista@2164
|
900 |
);
|
krista@2164
|
901 |
|
krista@2164
|
902 |
|
roker@529
|
903 |
sqlite3_busy_timeout(_session->db, BUSY_WAIT_TIME);
|
vb@0
|
904 |
|
krista@2480
|
905 |
#if _PEP_SQLITE_DEBUG
|
krista@2479
|
906 |
sqlite3_trace_v2(_session->db,
|
krista@2479
|
907 |
SQLITE_TRACE_STMT | SQLITE_TRACE_ROW | SQLITE_TRACE_CLOSE,
|
krista@2479
|
908 |
sql_trace_callback,
|
krista@2479
|
909 |
NULL);
|
krista@2480
|
910 |
#endif
|
krista@2479
|
911 |
|
vb@0
|
912 |
assert(SYSTEM_DB);
|
vb@0
|
913 |
if (SYSTEM_DB == NULL) {
|
roker@529
|
914 |
status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
|
vb@2834
|
915 |
goto pEp_error;
|
vb@0
|
916 |
}
|
vb@0
|
917 |
|
roker@529
|
918 |
int_result = sqlite3_open_v2(
|
roker@529
|
919 |
SYSTEM_DB, &_session->system_db,
|
roker@529
|
920 |
SQLITE_OPEN_READONLY
|
roker@529
|
921 |
| SQLITE_OPEN_FULLMUTEX
|
roker@529
|
922 |
| SQLITE_OPEN_SHAREDCACHE,
|
roker@529
|
923 |
NULL
|
roker@529
|
924 |
);
|
vb@0
|
925 |
|
roker@529
|
926 |
if (int_result != SQLITE_OK) {
|
roker@529
|
927 |
status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
|
vb@2834
|
928 |
goto pEp_error;
|
roker@529
|
929 |
}
|
vb@0
|
930 |
|
roker@529
|
931 |
sqlite3_busy_timeout(_session->system_db, 1000);
|
vb@0
|
932 |
|
vb@928
|
933 |
// increment this when patching DDL
|
krista@3729
|
934 |
#define _DDL_USER_VERSION "11"
|
vb@928
|
935 |
|
vb@62
|
936 |
if (in_first) {
|
vb@1091
|
937 |
|
vb@62
|
938 |
int_result = sqlite3_exec(
|
vb@62
|
939 |
_session->db,
|
vb@1008
|
940 |
"create table if not exists version_info (\n"
|
vb@456
|
941 |
" id integer primary key,\n"
|
vb@1085
|
942 |
" timestamp integer default (datetime('now')),\n"
|
vb@456
|
943 |
" version text,\n"
|
vb@456
|
944 |
" comment text\n"
|
vb@928
|
945 |
");\n",
|
vb@928
|
946 |
NULL,
|
vb@928
|
947 |
NULL,
|
vb@928
|
948 |
NULL
|
vb@928
|
949 |
);
|
vb@928
|
950 |
int_result = sqlite3_exec(
|
vb@928
|
951 |
_session->db,
|
vb@948
|
952 |
"PRAGMA application_id = 0x23423423;\n"
|
vb@456
|
953 |
"create table if not exists log (\n"
|
vb@1085
|
954 |
" timestamp integer default (datetime('now')),\n"
|
vb@456
|
955 |
" title text not null,\n"
|
krista@2176
|
956 |
" description text,\n"
|
vb@456
|
957 |
" entity text not null,\n"
|
vb@456
|
958 |
" comment text\n"
|
vb@456
|
959 |
");\n"
|
vb@456
|
960 |
"create index if not exists log_timestamp on log (\n"
|
vb@456
|
961 |
" timestamp\n"
|
vb@456
|
962 |
");\n"
|
vb@456
|
963 |
"create table if not exists pgp_keypair (\n"
|
vb@456
|
964 |
" fpr text primary key,\n"
|
vb@456
|
965 |
" created integer,\n"
|
vb@456
|
966 |
" expires integer,\n"
|
vb@944
|
967 |
" comment text,\n"
|
vb@1085
|
968 |
" flags integer default 0\n"
|
vb@456
|
969 |
");\n"
|
vb@456
|
970 |
"create index if not exists pgp_keypair_expires on pgp_keypair (\n"
|
vb@456
|
971 |
" expires\n"
|
vb@456
|
972 |
");\n"
|
vb@456
|
973 |
"create table if not exists person (\n"
|
vb@456
|
974 |
" id text primary key,\n"
|
vb@456
|
975 |
" username text not null,\n"
|
vb@456
|
976 |
" main_key_id text\n"
|
vb@456
|
977 |
" references pgp_keypair (fpr)\n"
|
vb@456
|
978 |
" on delete set null,\n"
|
vb@456
|
979 |
" lang text,\n"
|
vb@951
|
980 |
" comment text,\n"
|
krista@3420
|
981 |
// " device_group text,\n"
|
vb@2833
|
982 |
" is_pEp_user integer default 0\n"
|
vb@456
|
983 |
");\n"
|
vb@456
|
984 |
"create table if not exists identity (\n"
|
Edouard@559
|
985 |
" address text,\n"
|
vb@456
|
986 |
" user_id text\n"
|
vb@456
|
987 |
" references person (id)\n"
|
krista@2461
|
988 |
" on delete cascade on update cascade,\n"
|
vb@456
|
989 |
" main_key_id text\n"
|
vb@456
|
990 |
" references pgp_keypair (fpr)\n"
|
vb@456
|
991 |
" on delete set null,\n"
|
Edouard@559
|
992 |
" comment text,\n"
|
krista@2461
|
993 |
" flags integer default 0,\n"
|
krista@2461
|
994 |
" is_own integer default 0,\n"
|
krista@3730
|
995 |
" pEp_version_major integer default 0,\n"
|
krista@3730
|
996 |
" pEp_version_minor integer default 0,\n"
|
krista@2506
|
997 |
" timestamp integer default (datetime('now')),\n"
|
Edouard@559
|
998 |
" primary key (address, user_id)\n"
|
vb@456
|
999 |
");\n"
|
vb@456
|
1000 |
"create table if not exists trust (\n"
|
vb@456
|
1001 |
" user_id text not null\n"
|
vb@456
|
1002 |
" references person (id)\n"
|
krista@2461
|
1003 |
" on delete cascade on update cascade,\n"
|
vb@456
|
1004 |
" pgp_keypair_fpr text not null\n"
|
vb@456
|
1005 |
" references pgp_keypair (fpr)\n"
|
vb@456
|
1006 |
" on delete cascade,\n"
|
vb@456
|
1007 |
" comm_type integer not null,\n"
|
Edouard@684
|
1008 |
" comment text,\n"
|
Edouard@684
|
1009 |
" primary key (user_id, pgp_keypair_fpr)\n"
|
vb@456
|
1010 |
");\n"
|
fdik@494
|
1011 |
// blacklist
|
vb@456
|
1012 |
"create table if not exists blacklist_keys (\n"
|
vb@456
|
1013 |
" fpr text primary key\n"
|
vb@456
|
1014 |
");\n"
|
vb@632
|
1015 |
// sequences
|
vb@632
|
1016 |
"create table if not exists sequences(\n"
|
vb@632
|
1017 |
" name text primary key,\n"
|
vb@2830
|
1018 |
" value integer default 0\n"
|
vb@632
|
1019 |
");\n"
|
Edouard@693
|
1020 |
"create table if not exists revoked_keys (\n"
|
Edouard@693
|
1021 |
" revoked_fpr text primary key,\n"
|
Edouard@693
|
1022 |
" replacement_fpr text not null\n"
|
Edouard@693
|
1023 |
" references pgp_keypair (fpr)\n"
|
Edouard@693
|
1024 |
" on delete cascade,\n"
|
Edouard@693
|
1025 |
" revocation_date integer\n"
|
Edouard@693
|
1026 |
");\n"
|
krista@2461
|
1027 |
// user id aliases
|
krista@2461
|
1028 |
"create table if not exists alternate_user_id (\n"
|
krista@2478
|
1029 |
" default_id text references person (id)\n"
|
krista@2478
|
1030 |
" on delete cascade on update cascade,\n"
|
krista@2461
|
1031 |
" alternate_id text primary key\n"
|
krista@2461
|
1032 |
");\n"
|
krista@2467
|
1033 |
// mistrusted keys
|
krista@2467
|
1034 |
"create table if not exists mistrusted_keys (\n"
|
krista@2467
|
1035 |
" fpr text primary key\n"
|
krista@2467
|
1036 |
");\n"
|
krista@2739
|
1037 |
// social graph for key resets
|
krista@2739
|
1038 |
"create table if not exists social_graph (\n"
|
krista@2747
|
1039 |
" own_userid text,\n"
|
krista@2739
|
1040 |
" own_address text,\n"
|
krista@2752
|
1041 |
" contact_userid text,\n"
|
krista@2752
|
1042 |
" CONSTRAINT fk_own_identity\n"
|
krista@2747
|
1043 |
" FOREIGN KEY(own_address, own_userid)\n"
|
krista@2747
|
1044 |
" REFERENCES identity(address, user_id)\n"
|
krista@2862
|
1045 |
" ON DELETE CASCADE ON UPDATE CASCADE\n"
|
krista@2752
|
1046 |
");\n"
|
krista@2752
|
1047 |
// list of user_ids sent revocation
|
krista@2752
|
1048 |
"create table if not exists revocation_contact_list (\n"
|
krista@2752
|
1049 |
" fpr text not null references pgp_keypair (fpr)\n"
|
krista@2752
|
1050 |
" on delete cascade,\n"
|
krista@2752
|
1051 |
" contact_id text not null references person (id)\n"
|
krista@2752
|
1052 |
" on delete cascade on update cascade,\n"
|
krista@2752
|
1053 |
" timestamp integer default (datetime('now')),\n"
|
krista@2752
|
1054 |
" PRIMARY KEY(fpr, contact_id)\n"
|
krista@2739
|
1055 |
");\n"
|
vb@456
|
1056 |
,
|
vb@62
|
1057 |
NULL,
|
vb@62
|
1058 |
NULL,
|
vb@62
|
1059 |
NULL
|
vb@62
|
1060 |
);
|
vb@62
|
1061 |
assert(int_result == SQLITE_OK);
|
vb@0
|
1062 |
|
vb@928
|
1063 |
int version;
|
vb@62
|
1064 |
int_result = sqlite3_exec(
|
vb@62
|
1065 |
_session->db,
|
vb@928
|
1066 |
"pragma user_version;",
|
vb@928
|
1067 |
user_version,
|
vb@928
|
1068 |
&version,
|
vb@62
|
1069 |
NULL
|
vb@62
|
1070 |
);
|
krista@1884
|
1071 |
|
krista@1884
|
1072 |
assert(int_result == SQLITE_OK);
|
krista@1884
|
1073 |
|
krista@1884
|
1074 |
void (*xFunc_lower)(sqlite3_context*,int,sqlite3_value**) = &_sql_lower;
|
krista@1884
|
1075 |
|
krista@1884
|
1076 |
int_result = sqlite3_create_function_v2(
|
krista@1884
|
1077 |
_session->db,
|
krista@1884
|
1078 |
"lower",
|
krista@1884
|
1079 |
1,
|
krista@1884
|
1080 |
SQLITE_UTF8 | SQLITE_DETERMINISTIC,
|
krista@1884
|
1081 |
NULL,
|
krista@1884
|
1082 |
xFunc_lower,
|
krista@1884
|
1083 |
NULL,
|
krista@1884
|
1084 |
NULL,
|
krista@1884
|
1085 |
NULL);
|
vb@62
|
1086 |
assert(int_result == SQLITE_OK);
|
krista@2477
|
1087 |
|
krista@2477
|
1088 |
int_result = sqlite3_exec(
|
krista@2477
|
1089 |
_session->db,
|
krista@2477
|
1090 |
"pragma foreign_keys=ON;\n",
|
krista@2477
|
1091 |
NULL,
|
krista@2477
|
1092 |
NULL,
|
krista@2477
|
1093 |
NULL
|
krista@2477
|
1094 |
);
|
krista@2477
|
1095 |
|
krista@2477
|
1096 |
assert(int_result == SQLITE_OK);
|
krista@2477
|
1097 |
|
krista@2461
|
1098 |
|
krista@3331
|
1099 |
// Sometimes the user_version wasn't set correctly.
|
krista@2461
|
1100 |
if (version == 1) {
|
krista@2461
|
1101 |
bool version_changed = true;
|
krista@3730
|
1102 |
if (table_contains_column(_session, "identity", "pEp_version_major")) {
|
krista@3727
|
1103 |
version = 12;
|
krista@3729
|
1104 |
}
|
krista@3727
|
1105 |
else if (db_contains_table(_session, "social_graph") > 0) {
|
krista@3420
|
1106 |
if (!table_contains_column(_session, "person", "device_group"))
|
krista@3420
|
1107 |
version = 10;
|
krista@3420
|
1108 |
else
|
krista@3420
|
1109 |
version = 9;
|
krista@3331
|
1110 |
}
|
krista@3331
|
1111 |
else if (table_contains_column(_session, "identity", "timestamp") > 0) {
|
krista@2506
|
1112 |
version = 8;
|
krista@2506
|
1113 |
}
|
krista@3417
|
1114 |
else if (table_contains_column(_session, "person", "is_pEp_user") > 0) {
|
krista@2468
|
1115 |
version = 7;
|
krista@2468
|
1116 |
}
|
krista@2468
|
1117 |
else if (table_contains_column(_session, "identity", "is_own") > 0) {
|
krista@2461
|
1118 |
version = 6;
|
krista@2461
|
1119 |
}
|
krista@2461
|
1120 |
else if (table_contains_column(_session, "pgp_keypair", "flags") > 0) {
|
krista@2461
|
1121 |
version = 2;
|
krista@2461
|
1122 |
}
|
krista@2461
|
1123 |
else {
|
krista@2461
|
1124 |
version_changed = false;
|
krista@2461
|
1125 |
}
|
krista@2461
|
1126 |
|
krista@2461
|
1127 |
if (version_changed) {
|
krista@2461
|
1128 |
// set it in the DB, finally. Yeesh.
|
krista@2461
|
1129 |
char verbuf[21]; // enough digits for a max-sized 64 bit int, cmon.
|
krista@2461
|
1130 |
sprintf(verbuf,"%d",version);
|
krista@2461
|
1131 |
|
krista@2461
|
1132 |
size_t query_size = strlen(verbuf) + 25;
|
krista@2461
|
1133 |
char* query = calloc(query_size, 1);
|
krista@2461
|
1134 |
|
krista@2461
|
1135 |
strlcpy(query, "pragma user_version = ", query_size);
|
krista@2461
|
1136 |
strlcat(query, verbuf, query_size);
|
krista@2461
|
1137 |
strlcat(query, ";", query_size);
|
krista@2461
|
1138 |
|
krista@2461
|
1139 |
int_result = sqlite3_exec(
|
krista@2461
|
1140 |
_session->db,
|
krista@2461
|
1141 |
query,
|
krista@2461
|
1142 |
user_version,
|
krista@2461
|
1143 |
&version,
|
krista@2461
|
1144 |
NULL
|
krista@2461
|
1145 |
);
|
krista@2461
|
1146 |
free(query);
|
krista@2461
|
1147 |
}
|
krista@2461
|
1148 |
}
|
krista@2461
|
1149 |
|
vb@62
|
1150 |
|
edouard@1604
|
1151 |
if(version != 0) {
|
edouard@1604
|
1152 |
// Version has been already set
|
edouard@1604
|
1153 |
|
edouard@1604
|
1154 |
// Early mistake : version 0 shouldn't have existed.
|
edouard@1604
|
1155 |
// Numbering should have started at 1 to detect newly created DB.
|
edouard@1604
|
1156 |
// Version 0 DBs are not anymore compatible.
|
vb@928
|
1157 |
|
edouard@1604
|
1158 |
// // Was version 0 compat code.
|
edouard@1604
|
1159 |
// if (version < 1) {
|
edouard@1604
|
1160 |
// int_result = sqlite3_exec(
|
edouard@1604
|
1161 |
// _session->db,
|
edouard@1604
|
1162 |
// "alter table identity\n"
|
edouard@1604
|
1163 |
// " add column flags integer default 0;\n",
|
edouard@1604
|
1164 |
// NULL,
|
edouard@1604
|
1165 |
// NULL,
|
edouard@1604
|
1166 |
// NULL
|
edouard@1604
|
1167 |
// );
|
edouard@1604
|
1168 |
// assert(int_result == SQLITE_OK);
|
edouard@1604
|
1169 |
// }
|
krista@2461
|
1170 |
|
edouard@1604
|
1171 |
if (version < 2) {
|
krista@3420
|
1172 |
// N.B. addition of device_group column removed in DDL v10
|
edouard@1604
|
1173 |
int_result = sqlite3_exec(
|
edouard@1604
|
1174 |
_session->db,
|
edouard@1604
|
1175 |
"alter table pgp_keypair\n"
|
krista@3420
|
1176 |
" add column flags integer default 0;\n",
|
krista@3420
|
1177 |
// "alter table person\n"
|
krista@3420
|
1178 |
// " add column device_group text;\n",
|
edouard@1604
|
1179 |
NULL,
|
edouard@1604
|
1180 |
NULL,
|
edouard@1604
|
1181 |
NULL
|
edouard@1604
|
1182 |
);
|
edouard@1604
|
1183 |
assert(int_result == SQLITE_OK);
|
edouard@1604
|
1184 |
}
|
vb@928
|
1185 |
|
edouard@1604
|
1186 |
if (version < 5) {
|
edouard@1604
|
1187 |
int_result = sqlite3_exec(
|
edouard@1604
|
1188 |
_session->db,
|
edouard@1604
|
1189 |
"delete from pgp_keypair where fpr = '';",
|
edouard@1604
|
1190 |
NULL,
|
edouard@1604
|
1191 |
NULL,
|
edouard@1604
|
1192 |
NULL
|
edouard@1604
|
1193 |
);
|
edouard@1604
|
1194 |
assert(int_result == SQLITE_OK);
|
edouard@1604
|
1195 |
int_result = sqlite3_exec(
|
edouard@1604
|
1196 |
_session->db,
|
edouard@1604
|
1197 |
"delete from trust where pgp_keypair_fpr = '';",
|
edouard@1604
|
1198 |
NULL,
|
edouard@1604
|
1199 |
NULL,
|
edouard@1604
|
1200 |
NULL
|
edouard@1604
|
1201 |
);
|
edouard@1604
|
1202 |
assert(int_result == SQLITE_OK);
|
edouard@1604
|
1203 |
}
|
krista@2461
|
1204 |
|
krista@2461
|
1205 |
if (version < 6) {
|
krista@2461
|
1206 |
int_result = sqlite3_exec(
|
krista@2461
|
1207 |
_session->db,
|
krista@2461
|
1208 |
"alter table identity\n"
|
krista@2461
|
1209 |
" add column is_own integer default 0;\n",
|
krista@2461
|
1210 |
NULL,
|
krista@2461
|
1211 |
NULL,
|
krista@2461
|
1212 |
NULL
|
krista@2461
|
1213 |
);
|
krista@2461
|
1214 |
assert(int_result == SQLITE_OK);
|
krista@2461
|
1215 |
int_result = sqlite3_exec(
|
krista@2461
|
1216 |
_session->db,
|
krista@2461
|
1217 |
"update identity\n"
|
krista@2461
|
1218 |
" set is_own = 1\n"
|
krista@2461
|
1219 |
" where (user_id = '" PEP_OWN_USERID "');\n",
|
krista@2461
|
1220 |
NULL,
|
krista@2461
|
1221 |
NULL,
|
krista@2461
|
1222 |
NULL
|
krista@2461
|
1223 |
);
|
krista@2461
|
1224 |
assert(int_result == SQLITE_OK);
|
krista@2461
|
1225 |
|
krista@2461
|
1226 |
// Turns out that just adding "on update cascade" in
|
krista@2461
|
1227 |
// sqlite is a PITA. We need to be able to cascade
|
krista@2461
|
1228 |
// person->id replacements (for temp ids like "TOFU_")
|
krista@2461
|
1229 |
// so here we go...
|
krista@2461
|
1230 |
int_result = sqlite3_exec(
|
krista@2461
|
1231 |
_session->db,
|
krista@2461
|
1232 |
"PRAGMA foreign_keys=off;\n"
|
krista@2461
|
1233 |
"BEGIN TRANSACTION;\n"
|
krista@3462
|
1234 |
"create table _identity_new (\n"
|
krista@2461
|
1235 |
" address text,\n"
|
krista@2461
|
1236 |
" user_id text\n"
|
krista@2461
|
1237 |
" references person (id)\n"
|
krista@2461
|
1238 |
" on delete cascade on update cascade,\n"
|
krista@2461
|
1239 |
" main_key_id text\n"
|
krista@2461
|
1240 |
" references pgp_keypair (fpr)\n"
|
krista@2461
|
1241 |
" on delete set null,\n"
|
krista@2461
|
1242 |
" comment text,\n"
|
krista@2461
|
1243 |
" flags integer default 0,\n"
|
krista@2461
|
1244 |
" is_own integer default 0,\n"
|
krista@2461
|
1245 |
" primary key (address, user_id)\n"
|
krista@3462
|
1246 |
");\n"
|
krista@3462
|
1247 |
"INSERT INTO _identity_new SELECT * FROM identity;\n"
|
krista@3462
|
1248 |
"DROP TABLE identity;\n"
|
krista@3462
|
1249 |
"ALTER TABLE _identity_new RENAME TO identity;\n"
|
krista@3462
|
1250 |
"COMMIT;\n"
|
krista@3462
|
1251 |
"\n"
|
krista@3462
|
1252 |
"BEGIN TRANSACTION;\n"
|
krista@3462
|
1253 |
"create table _trust_new (\n"
|
krista@2461
|
1254 |
" user_id text not null\n"
|
krista@2461
|
1255 |
" references person (id)\n"
|
krista@2461
|
1256 |
" on delete cascade on update cascade,\n"
|
krista@2461
|
1257 |
" pgp_keypair_fpr text not null\n"
|
krista@2461
|
1258 |
" references pgp_keypair (fpr)\n"
|
krista@2461
|
1259 |
" on delete cascade,\n"
|
krista@2461
|
1260 |
" comm_type integer not null,\n"
|
krista@2461
|
1261 |
" comment text,\n"
|
krista@2461
|
1262 |
" primary key (user_id, pgp_keypair_fpr)\n"
|
krista@2461
|
1263 |
");\n"
|
krista@3462
|
1264 |
"INSERT INTO _trust_new SELECT * FROM trust;\n"
|
krista@3462
|
1265 |
"DROP TABLE trust;\n"
|
krista@3462
|
1266 |
"ALTER TABLE _trust_new RENAME TO trust;\n"
|
krista@2461
|
1267 |
"COMMIT;\n"
|
krista@2461
|
1268 |
"\n"
|
krista@2461
|
1269 |
"PRAGMA foreign_keys=on;\n"
|
krista@2461
|
1270 |
"create table if not exists alternate_user_id (\n"
|
krista@2478
|
1271 |
" default_id text references person (id)\n"
|
krista@2478
|
1272 |
" on delete cascade on update cascade,\n"
|
krista@2461
|
1273 |
" alternate_id text primary key\n"
|
krista@2461
|
1274 |
");\n"
|
krista@2461
|
1275 |
,
|
krista@2461
|
1276 |
NULL,
|
krista@2461
|
1277 |
NULL,
|
krista@2461
|
1278 |
NULL
|
krista@2461
|
1279 |
);
|
krista@3462
|
1280 |
assert(int_result == SQLITE_OK);
|
krista@3462
|
1281 |
|
krista@3462
|
1282 |
int_result = sqlite3_exec(
|
krista@3462
|
1283 |
_session->db,
|
krista@3462
|
1284 |
"PRAGMA foreign_key_check;\n"
|
krista@3462
|
1285 |
,
|
krista@3462
|
1286 |
NULL,
|
krista@3462
|
1287 |
NULL,
|
krista@3462
|
1288 |
NULL
|
krista@3462
|
1289 |
);
|
krista@3462
|
1290 |
assert(int_result == SQLITE_OK);
|
krista@3462
|
1291 |
|
krista@3462
|
1292 |
// FIXME: foreign key check here
|
krista@2461
|
1293 |
}
|
krista@2468
|
1294 |
if (version < 7) {
|
krista@2468
|
1295 |
int_result = sqlite3_exec(
|
krista@2468
|
1296 |
_session->db,
|
krista@2468
|
1297 |
"alter table person\n"
|
vb@2833
|
1298 |
" add column is_pEp_user integer default 0;\n",
|
krista@2468
|
1299 |
NULL,
|
krista@2468
|
1300 |
NULL,
|
krista@2468
|
1301 |
NULL
|
krista@2468
|
1302 |
);
|
krista@2468
|
1303 |
assert(int_result == SQLITE_OK);
|
krista@2468
|
1304 |
int_result = sqlite3_exec(
|
krista@2468
|
1305 |
_session->db,
|
krista@2468
|
1306 |
"update person\n"
|
vb@2833
|
1307 |
" set is_pEp_user = 1\n"
|
krista@2468
|
1308 |
" where id = "
|
krista@2468
|
1309 |
" (select distinct id from person "
|
krista@2468
|
1310 |
" join trust on id = user_id "
|
krista@2468
|
1311 |
" where (case when (comm_type = 127) then (id) "
|
krista@2468
|
1312 |
" when (comm_type = 255) then (id) "
|
krista@2468
|
1313 |
" else 0"
|
krista@2468
|
1314 |
" end) = id );\n",
|
krista@2468
|
1315 |
NULL,
|
krista@2468
|
1316 |
NULL,
|
krista@2468
|
1317 |
NULL
|
krista@2468
|
1318 |
);
|
krista@2473
|
1319 |
assert(int_result == SQLITE_OK);
|
krista@2473
|
1320 |
|
krista@2473
|
1321 |
int_result = sqlite3_exec(
|
krista@2473
|
1322 |
_session->db,
|
krista@2471
|
1323 |
"create table if not exists mistrusted_keys (\n"
|
krista@2471
|
1324 |
" fpr text primary key\n"
|
krista@2471
|
1325 |
");\n",
|
krista@2471
|
1326 |
NULL,
|
krista@2471
|
1327 |
NULL,
|
krista@2471
|
1328 |
NULL
|
krista@2471
|
1329 |
);
|
krista@2468
|
1330 |
assert(int_result == SQLITE_OK);
|
krista@2468
|
1331 |
}
|
krista@2506
|
1332 |
if (version < 8) {
|
krista@2506
|
1333 |
int_result = sqlite3_exec(
|
krista@2506
|
1334 |
_session->db,
|
krista@2509
|
1335 |
"PRAGMA foreign_keys=off;\n"
|
krista@2509
|
1336 |
"BEGIN TRANSACTION;\n"
|
krista@3462
|
1337 |
"create table _identity_new (\n"
|
krista@2509
|
1338 |
" address text,\n"
|
krista@2509
|
1339 |
" user_id text\n"
|
krista@2509
|
1340 |
" references person (id)\n"
|
krista@2509
|
1341 |
" on delete cascade on update cascade,\n"
|
krista@2509
|
1342 |
" main_key_id text\n"
|
krista@2509
|
1343 |
" references pgp_keypair (fpr)\n"
|
krista@2509
|
1344 |
" on delete set null,\n"
|
krista@2509
|
1345 |
" comment text,\n"
|
krista@2509
|
1346 |
" flags integer default 0,\n"
|
krista@2509
|
1347 |
" is_own integer default 0,\n"
|
krista@2509
|
1348 |
" timestamp integer default (datetime('now')),\n"
|
krista@2509
|
1349 |
" primary key (address, user_id)\n"
|
krista@2509
|
1350 |
");\n"
|
krista@3462
|
1351 |
"INSERT INTO _identity_new (address, user_id, main_key_id, "
|
krista@2514
|
1352 |
" comment, flags, is_own) "
|
krista@3463
|
1353 |
" SELECT identity.address, identity.user_id, "
|
krista@3463
|
1354 |
" identity.main_key_id, identity.comment, "
|
krista@3463
|
1355 |
" identity.flags, identity.is_own "
|
krista@3462
|
1356 |
" FROM identity "
|
krista@2514
|
1357 |
" WHERE 1;\n"
|
krista@3462
|
1358 |
"DROP TABLE identity;\n"
|
krista@3462
|
1359 |
"ALTER TABLE _identity_new RENAME TO identity;\n"
|
krista@2509
|
1360 |
"COMMIT;\n"
|
krista@2509
|
1361 |
"\n"
|
krista@2509
|
1362 |
"PRAGMA foreign_keys=on;\n"
|
krista@2509
|
1363 |
,
|
krista@2506
|
1364 |
NULL,
|
krista@2506
|
1365 |
NULL,
|
krista@2506
|
1366 |
NULL
|
krista@2506
|
1367 |
);
|
krista@2509
|
1368 |
assert(int_result == SQLITE_OK);
|
krista@3462
|
1369 |
|
krista@3462
|
1370 |
int_result = sqlite3_exec(
|
krista@3462
|
1371 |
_session->db,
|
krista@3462
|
1372 |
"PRAGMA foreign_key_check;\n"
|
krista@3462
|
1373 |
,
|
krista@3462
|
1374 |
NULL,
|
krista@3462
|
1375 |
NULL,
|
krista@3462
|
1376 |
NULL
|
krista@3462
|
1377 |
);
|
krista@3462
|
1378 |
assert(int_result == SQLITE_OK);
|
krista@3462
|
1379 |
|
krista@3462
|
1380 |
// FIXME: foreign key check
|
krista@2506
|
1381 |
}
|
krista@2747
|
1382 |
if (version < 9) {
|
krista@2747
|
1383 |
int_result = sqlite3_exec(
|
krista@2747
|
1384 |
_session->db,
|
krista@2747
|
1385 |
"create table if not exists social_graph (\n"
|
krista@2747
|
1386 |
" own_userid text,\n"
|
krista@2747
|
1387 |
" own_address text,\n"
|
krista@2752
|
1388 |
" contact_userid text,\n"
|
krista@2752
|
1389 |
" CONSTRAINT fk_own_identity\n"
|
krista@2747
|
1390 |
" FOREIGN KEY(own_address, own_userid)\n"
|
krista@2747
|
1391 |
" REFERENCES identity(address, user_id)\n"
|
krista@3331
|
1392 |
" ON DELETE CASCADE ON UPDATE CASCADE\n"
|
krista@2752
|
1393 |
");\n"
|
krista@2752
|
1394 |
"create table if not exists revocation_contact_list (\n"
|
krista@2752
|
1395 |
" fpr text not null references pgp_keypair (fpr)\n"
|
krista@2752
|
1396 |
" on delete cascade,\n"
|
krista@2752
|
1397 |
" contact_id text not null references person (id)\n"
|
krista@2752
|
1398 |
" on delete cascade on update cascade,\n"
|
krista@2752
|
1399 |
" timestamp integer default (datetime('now')),\n"
|
krista@2752
|
1400 |
" PRIMARY KEY(fpr, contact_id)\n"
|
krista@2747
|
1401 |
");\n"
|
krista@2747
|
1402 |
,
|
krista@2747
|
1403 |
NULL,
|
krista@2747
|
1404 |
NULL,
|
krista@2747
|
1405 |
NULL
|
krista@2747
|
1406 |
);
|
krista@2747
|
1407 |
assert(int_result == SQLITE_OK);
|
krista@2747
|
1408 |
}
|
krista@3420
|
1409 |
if (version < 10 && version > 1) {
|
krista@3420
|
1410 |
int_result = sqlite3_exec(
|
krista@3460
|
1411 |
_session->db,
|
krista@3420
|
1412 |
"PRAGMA foreign_keys=off;\n"
|
krista@3420
|
1413 |
"BEGIN TRANSACTION;\n"
|
krista@3462
|
1414 |
"create table _person_new (\n"
|
krista@3420
|
1415 |
" id text primary key,\n"
|
krista@3420
|
1416 |
" username text not null,\n"
|
krista@3420
|
1417 |
" main_key_id text\n"
|
krista@3420
|
1418 |
" references pgp_keypair (fpr)\n"
|
krista@3420
|
1419 |
" on delete set null,\n"
|
krista@3420
|
1420 |
" lang text,\n"
|
krista@3420
|
1421 |
" comment text,\n"
|
krista@3420
|
1422 |
" is_pEp_user integer default 0\n"
|
krista@3420
|
1423 |
");\n"
|
krista@3462
|
1424 |
"INSERT INTO _person_new (id, username, main_key_id, "
|
krista@3420
|
1425 |
" lang, comment, is_pEp_user) "
|
krista@3462
|
1426 |
" SELECT person.id, person.username, "
|
krista@3462
|
1427 |
" person.main_key_id, person.lang, "
|
krista@3462
|
1428 |
" person.comment, person.is_pEp_user "
|
krista@3462
|
1429 |
" FROM person "
|
krista@3420
|
1430 |
" WHERE 1;\n"
|
krista@3462
|
1431 |
"DROP TABLE person;\n"
|
krista@3462
|
1432 |
"ALTER TABLE _person_new RENAME TO person;\n"
|
krista@3420
|
1433 |
"COMMIT;\n"
|
krista@3420
|
1434 |
"\n"
|
krista@3420
|
1435 |
"PRAGMA foreign_keys=on;\n"
|
krista@3420
|
1436 |
,
|
krista@3420
|
1437 |
NULL,
|
krista@3420
|
1438 |
NULL,
|
krista@3420
|
1439 |
NULL
|
krista@3420
|
1440 |
);
|
krista@3420
|
1441 |
assert(int_result == SQLITE_OK);
|
krista@3462
|
1442 |
int_result = sqlite3_exec(
|
krista@3462
|
1443 |
_session->db,
|
krista@3462
|
1444 |
"PRAGMA foreign_key_check;\n"
|
krista@3462
|
1445 |
,
|
krista@3462
|
1446 |
NULL,
|
krista@3462
|
1447 |
NULL,
|
krista@3462
|
1448 |
NULL
|
krista@3462
|
1449 |
);
|
krista@3462
|
1450 |
assert(int_result == SQLITE_OK);
|
krista@3420
|
1451 |
}
|
krista@3463
|
1452 |
if (version < 11) {
|
krista@3463
|
1453 |
status = repair_altered_tables(_session);
|
krista@3463
|
1454 |
assert(status == PEP_STATUS_OK);
|
krista@3463
|
1455 |
if (status != PEP_STATUS_OK)
|
krista@3463
|
1456 |
return status;
|
krista@3463
|
1457 |
}
|
krista@3727
|
1458 |
if (version < 12) {
|
krista@3727
|
1459 |
int_result = sqlite3_exec(
|
krista@3727
|
1460 |
_session->db,
|
krista@3727
|
1461 |
"alter table identity\n"
|
krista@3730
|
1462 |
" add column pEp_version_major integer default 0;\n"
|
krista@3730
|
1463 |
"alter table identity\n"
|
krista@3730
|
1464 |
" add column pEp_version_minor integer default 0;\n",
|
krista@3730
|
1465 |
NULL,
|
krista@3730
|
1466 |
NULL,
|
krista@3730
|
1467 |
NULL
|
krista@3730
|
1468 |
);
|
krista@3730
|
1469 |
if (status != PEP_STATUS_OK)
|
krista@3730
|
1470 |
return status;
|
krista@3730
|
1471 |
|
krista@3730
|
1472 |
int_result = sqlite3_exec(
|
krista@3730
|
1473 |
_session->db,
|
krista@3730
|
1474 |
"update identity\n"
|
krista@3730
|
1475 |
" set pEp_version_major = 2\n"
|
krista@3730
|
1476 |
" where exists (select * from person\n"
|
krista@3730
|
1477 |
" where identity.user_id = person.id\n"
|
krista@3730
|
1478 |
" and identity.is_own = 0\n"
|
krista@3730
|
1479 |
" and person.is_pEp_user = 1);\n",
|
krista@3727
|
1480 |
NULL,
|
krista@3727
|
1481 |
NULL,
|
krista@3727
|
1482 |
NULL
|
krista@3727
|
1483 |
);
|
krista@3729
|
1484 |
if (status != PEP_STATUS_OK)
|
krista@3730
|
1485 |
return status;
|
krista@3730
|
1486 |
|
krista@3730
|
1487 |
// N.B. WE DEFINE PEP_VERSION - IF WE'RE AT 9-DIGIT MAJOR OR MINOR VERSIONS, ER, BAD.
|
krista@3730
|
1488 |
char major_buf[10];
|
krista@3730
|
1489 |
char minor_buf[10];
|
krista@3730
|
1490 |
if (sscanf("%s.%s", major_buf, minor_buf) != 2)
|
krista@3730
|
1491 |
return PEP_UNKNOWN_ERROR; // DO BETTER
|
krista@3730
|
1492 |
size_t major_len = strlen(major_buf);
|
krista@3730
|
1493 |
size_t minor_len = strlen(minor_buf);
|
krista@3730
|
1494 |
|
krista@3730
|
1495 |
const char* _ver_12_startstr =
|
krista@3730
|
1496 |
"update identity\n"
|
krista@3730
|
1497 |
" set pEp_version_major = ";
|
krista@3730
|
1498 |
const char* _ver_12_midstr = ",\n"
|
krista@3730
|
1499 |
" pEp_version_minor = ";
|
krista@3730
|
1500 |
const char* _ver_12_endstr =
|
krista@3730
|
1501 |
"\n"
|
krista@3730
|
1502 |
" where identity.is_own = 1;\n";
|
krista@3730
|
1503 |
|
krista@3730
|
1504 |
size_t new_stringlen = strlen(_ver_12_startstr) + major_len +
|
krista@3730
|
1505 |
strlen(_ver_12_midstr) + minor_len +
|
krista@3730
|
1506 |
strlen(_ver_12_endstr);
|
krista@3730
|
1507 |
|
krista@3730
|
1508 |
char* _ver_12_stmt = calloc(new_stringlen + 1, 1);
|
krista@3730
|
1509 |
snprintf(_ver_12_stmt, new_stringlen + 1, "%s%s%s%s%s",
|
krista@3730
|
1510 |
_ver_12_startstr, major_buf, _ver_12_midstr, minor_buf, _ver_12_endstr);
|
krista@3730
|
1511 |
|
krista@3730
|
1512 |
int_result = sqlite3_exec(
|
krista@3730
|
1513 |
_session->db,
|
krista@3730
|
1514 |
_ver_12_stmt,
|
krista@3730
|
1515 |
NULL,
|
krista@3730
|
1516 |
NULL,
|
krista@3730
|
1517 |
NULL
|
krista@3730
|
1518 |
);
|
krista@3730
|
1519 |
free(_ver_12_stmt);
|
krista@3730
|
1520 |
if (status != PEP_STATUS_OK)
|
krista@3730
|
1521 |
return status;
|
krista@3727
|
1522 |
}
|
krista@2468
|
1523 |
}
|
edouard@1752
|
1524 |
else {
|
edouard@1752
|
1525 |
// Version from DB was 0, it means this is initial setup.
|
edouard@1752
|
1526 |
// DB has just been created, and all tables are empty.
|
edouard@1752
|
1527 |
very_first = true;
|
edouard@1752
|
1528 |
}
|
vb@1453
|
1529 |
|
vb@928
|
1530 |
if (version < atoi(_DDL_USER_VERSION)) {
|
vb@928
|
1531 |
int_result = sqlite3_exec(
|
vb@928
|
1532 |
_session->db,
|
krista@2788
|
1533 |
"pragma user_version = "_DDL_USER_VERSION";\n"
|
krista@2788
|
1534 |
"insert or replace into version_info (id, version)"
|
krista@2788
|
1535 |
"values (1, '" PEP_ENGINE_VERSION "');",
|
vb@928
|
1536 |
NULL,
|
vb@928
|
1537 |
NULL,
|
vb@928
|
1538 |
NULL
|
vb@928
|
1539 |
);
|
vb@928
|
1540 |
assert(int_result == SQLITE_OK);
|
vb@928
|
1541 |
}
|
krista@2176
|
1542 |
|
krista@2176
|
1543 |
// We need to init a few globals for message id that we'd rather not
|
krista@2176
|
1544 |
// calculate more than once.
|
krista@2176
|
1545 |
_init_globals();
|
vb@62
|
1546 |
}
|
vb@62
|
1547 |
|
vb@951
|
1548 |
int_result = sqlite3_prepare_v2(_session->db, sql_log,
|
vb@951
|
1549 |
(int)strlen(sql_log), &_session->log, NULL);
|
roker@529
|
1550 |
assert(int_result == SQLITE_OK);
|
vb@0
|
1551 |
|
vb@233
|
1552 |
int_result = sqlite3_prepare_v2(_session->system_db, sql_trustword,
|
Edouard@417
|
1553 |
(int)strlen(sql_trustword), &_session->trustword, NULL);
|
roker@529
|
1554 |
assert(int_result == SQLITE_OK);
|
vb@0
|
1555 |
|
vb@0
|
1556 |
int_result = sqlite3_prepare_v2(_session->db, sql_get_identity,
|
Edouard@417
|
1557 |
(int)strlen(sql_get_identity), &_session->get_identity, NULL);
|
roker@529
|
1558 |
assert(int_result == SQLITE_OK);
|
vb@0
|
1559 |
|
krista@2461
|
1560 |
int_result = sqlite3_prepare_v2(_session->db, sql_get_identity_without_trust_check,
|
krista@2461
|
1561 |
(int)strlen(sql_get_identity_without_trust_check),
|
krista@2461
|
1562 |
&_session->get_identity_without_trust_check, NULL);
|
krista@2461
|
1563 |
assert(int_result == SQLITE_OK);
|
krista@2461
|
1564 |
|
krista@2461
|
1565 |
int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_address,
|
krista@2461
|
1566 |
(int)strlen(sql_get_identities_by_address),
|
krista@2461
|
1567 |
&_session->get_identities_by_address, NULL);
|
krista@2461
|
1568 |
assert(int_result == SQLITE_OK);
|
krista@2966
|
1569 |
|
krista@2966
|
1570 |
int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_userid,
|
krista@2966
|
1571 |
(int)strlen(sql_get_identities_by_userid),
|
krista@2966
|
1572 |
&_session->get_identities_by_userid, NULL);
|
krista@2966
|
1573 |
assert(int_result == SQLITE_OK);
|
krista@2461
|
1574 |
|
krista@2893
|
1575 |
int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_main_key_id,
|
krista@2893
|
1576 |
(int)strlen(sql_get_identities_by_main_key_id),
|
krista@2893
|
1577 |
&_session->get_identities_by_main_key_id, NULL);
|
krista@2893
|
1578 |
assert(int_result == SQLITE_OK);
|
krista@2893
|
1579 |
|
krista@2461
|
1580 |
int_result = sqlite3_prepare_v2(_session->db, sql_get_user_default_key,
|
krista@2461
|
1581 |
(int)strlen(sql_get_user_default_key), &_session->get_user_default_key, NULL);
|
krista@2461
|
1582 |
assert(int_result == SQLITE_OK);
|
krista@2461
|
1583 |
|
krista@2893
|
1584 |
int_result = sqlite3_prepare_v2(_session->db, sql_get_all_keys_for_user,
|
krista@2893
|
1585 |
(int)strlen(sql_get_all_keys_for_user), &_session->get_all_keys_for_user, NULL);
|
krista@2893
|
1586 |
assert(int_result == SQLITE_OK);
|
krista@2893
|
1587 |
|
krista@2461
|
1588 |
int_result = sqlite3_prepare_v2(_session->db, sql_get_default_own_userid,
|
krista@2461
|
1589 |
(int)strlen(sql_get_default_own_userid), &_session->get_default_own_userid, NULL);
|
krista@2461
|
1590 |
assert(int_result == SQLITE_OK);
|
krista@2461
|
1591 |
|
krista@2461
|
1592 |
int_result = sqlite3_prepare_v2(_session->db, sql_get_userid_alias_default,
|
krista@2461
|
1593 |
(int)strlen(sql_get_userid_alias_default), &_session->get_userid_alias_default, NULL);
|
krista@2461
|
1594 |
assert(int_result == SQLITE_OK);
|
krista@2461
|
1595 |
|
krista@2461
|
1596 |
int_result = sqlite3_prepare_v2(_session->db, sql_add_userid_alias,
|
krista@2461
|
1597 |
(int)strlen(sql_add_userid_alias), &_session->add_userid_alias, NULL);
|
krista@2461
|
1598 |
assert(int_result == SQLITE_OK);
|
krista@2461
|
1599 |
|
krista@2461
|
1600 |
int_result = sqlite3_prepare_v2(_session->db, sql_replace_userid,
|
krista@2461
|
1601 |
(int)strlen(sql_replace_userid), &_session->replace_userid, NULL);
|
krista@2461
|
1602 |
assert(int_result == SQLITE_OK);
|
krista@2461
|
1603 |
|
krista@2948
|
1604 |
int_result = sqlite3_prepare_v2(_session->db, sql_delete_key,
|
krista@2948
|
1605 |
(int)strlen(sql_delete_key), &_session->delete_key, NULL);
|
krista@2948
|
1606 |
assert(int_result == SQLITE_OK);
|
krista@2948
|
1607 |
|
krista@2461
|
1608 |
int_result = sqlite3_prepare_v2(_session->db, sql_replace_main_user_fpr,
|
krista@2461
|
1609 |
(int)strlen(sql_replace_main_user_fpr), &_session->replace_main_user_fpr, NULL);
|
krista@2461
|
1610 |
assert(int_result == SQLITE_OK);
|
krista@2461
|
1611 |
|
krista@2461
|
1612 |
int_result = sqlite3_prepare_v2(_session->db, sql_get_main_user_fpr,
|
krista@2461
|
1613 |
(int)strlen(sql_get_main_user_fpr), &_session->get_main_user_fpr, NULL);
|
krista@2461
|
1614 |
assert(int_result == SQLITE_OK);
|
krista@2461
|
1615 |
|
krista@2461
|
1616 |
int_result = sqlite3_prepare_v2(_session->db, sql_refresh_userid_default_key,
|
krista@2461
|
1617 |
(int)strlen(sql_refresh_userid_default_key), &_session->refresh_userid_default_key, NULL);
|
krista@2461
|
1618 |
assert(int_result == SQLITE_OK);
|
krista@2461
|
1619 |
|
krista@1799
|
1620 |
int_result = sqlite3_prepare_v2(_session->db, sql_replace_identities_fpr,
|
krista@1799
|
1621 |
(int)strlen(sql_replace_identities_fpr),
|
krista@1799
|
1622 |
&_session->replace_identities_fpr, NULL);
|
krista@1793
|
1623 |
assert(int_result == SQLITE_OK);
|
krista@2461
|
1624 |
|
krista@2461
|
1625 |
int_result = sqlite3_prepare_v2(_session->db, sql_remove_fpr_as_default,
|
krista@2461
|
1626 |
(int)strlen(sql_remove_fpr_as_default),
|
krista@2461
|
1627 |
&_session->remove_fpr_as_default, NULL);
|
krista@2461
|
1628 |
assert(int_result == SQLITE_OK);
|
krista@1793
|
1629 |
|
vb@0
|
1630 |
int_result = sqlite3_prepare_v2(_session->db, sql_set_person,
|
Edouard@417
|
1631 |
(int)strlen(sql_set_person), &_session->set_person, NULL);
|
vb@0
|
1632 |
assert(int_result == SQLITE_OK);
|
vb@62
|
1633 |
|
krista@2478
|
1634 |
int_result = sqlite3_prepare_v2(_session->db, sql_update_person,
|
krista@2478
|
1635 |
(int)strlen(sql_update_person), &_session->update_person, NULL);
|
krista@2478
|
1636 |
assert(int_result == SQLITE_OK);
|
krista@2478
|
1637 |
|
krista@2965
|
1638 |
int_result = sqlite3_prepare_v2(_session->db, sql_delete_person,
|
krista@2965
|
1639 |
(int)strlen(sql_delete_person), &_session->delete_person, NULL);
|
krista@2965
|
1640 |
assert(int_result == SQLITE_OK);
|
krista@2965
|
1641 |
|
krista@2480
|
1642 |
int_result = sqlite3_prepare_v2(_session->db, sql_exists_person,
|
krista@2480
|
1643 |
(int)strlen(sql_exists_person), &_session->exists_person, NULL);
|
krista@2480
|
1644 |
assert(int_result == SQLITE_OK);
|
krista@2480
|
1645 |
|
vb@2833
|
1646 |
int_result = sqlite3_prepare_v2(_session->db, sql_set_as_pEp_user,
|
vb@2833
|
1647 |
(int)strlen(sql_set_as_pEp_user), &_session->set_as_pEp_user, NULL);
|
krista@2468
|
1648 |
assert(int_result == SQLITE_OK);
|
krista@2468
|
1649 |
|
vb@2833
|
1650 |
int_result = sqlite3_prepare_v2(_session->db, sql_is_pEp_user,
|
vb@2833
|
1651 |
(int)strlen(sql_is_pEp_user), &_session->is_pEp_user, NULL);
|
krista@2468
|
1652 |
assert(int_result == SQLITE_OK);
|
krista@2468
|
1653 |
|
krista@2747
|
1654 |
int_result = sqlite3_prepare_v2(_session->db, sql_add_into_social_graph,
|
krista@2747
|
1655 |
(int)strlen(sql_add_into_social_graph), &_session->add_into_social_graph, NULL);
|
krista@2747
|
1656 |
assert(int_result == SQLITE_OK);
|
krista@2747
|
1657 |
|
krista@2752
|
1658 |
int_result = sqlite3_prepare_v2(_session->db,
|
krista@2752
|
1659 |
sql_get_own_address_binding_from_contact,
|
krista@2752
|
1660 |
(int)strlen(sql_get_own_address_binding_from_contact),
|
krista@2752
|
1661 |
&_session->get_own_address_binding_from_contact, NULL);
|
krista@2752
|
1662 |
assert(int_result == SQLITE_OK);
|
krista@2752
|
1663 |
|
krista@2752
|
1664 |
int_result = sqlite3_prepare_v2(_session->db,
|
krista@2752
|
1665 |
sql_set_revoke_contact_as_notified,
|
krista@2752
|
1666 |
(int)strlen(sql_set_revoke_contact_as_notified),
|
krista@2752
|
1667 |
&_session->set_revoke_contact_as_notified, NULL);
|
krista@2752
|
1668 |
assert(int_result == SQLITE_OK);
|
krista@2752
|
1669 |
|
krista@2752
|
1670 |
int_result = sqlite3_prepare_v2(_session->db,
|
krista@2752
|
1671 |
sql_get_contacted_ids_from_revoke_fpr,
|
krista@2752
|
1672 |
(int)strlen(sql_get_contacted_ids_from_revoke_fpr),
|
krista@2752
|
1673 |
&_session->get_contacted_ids_from_revoke_fpr, NULL);
|
krista@2752
|
1674 |
assert(int_result == SQLITE_OK);
|
krista@2752
|
1675 |
|
krista@2752
|
1676 |
int_result = sqlite3_prepare_v2(_session->db,
|
krista@2800
|
1677 |
sql_was_id_for_revoke_contacted,
|
krista@2800
|
1678 |
(int)strlen(sql_was_id_for_revoke_contacted),
|
krista@2800
|
1679 |
&_session->was_id_for_revoke_contacted, NULL);
|
krista@2800
|
1680 |
assert(int_result == SQLITE_OK);
|
krista@2800
|
1681 |
|
krista@2800
|
1682 |
int_result = sqlite3_prepare_v2(_session->db,
|
krista@2800
|
1683 |
sql_get_last_contacted,
|
krista@2800
|
1684 |
(int)strlen(sql_get_last_contacted),
|
krista@2800
|
1685 |
&_session->get_last_contacted, NULL);
|
krista@2800
|
1686 |
assert(int_result == SQLITE_OK);
|
krista@2800
|
1687 |
|
krista@2800
|
1688 |
int_result = sqlite3_prepare_v2(_session->db,
|
krista@2752
|
1689 |
sql_get_own_address_binding_from_contact,
|
krista@2752
|
1690 |
(int)strlen(sql_get_own_address_binding_from_contact),
|
krista@2752
|
1691 |
&_session->get_own_address_binding_from_contact, NULL);
|
krista@2468
|
1692 |
assert(int_result == SQLITE_OK);
|
krista@2468
|
1693 |
|
vb@0
|
1694 |
int_result = sqlite3_prepare_v2(_session->db, sql_set_pgp_keypair,
|
vb@951
|
1695 |
(int)strlen(sql_set_pgp_keypair), &_session->set_pgp_keypair,
|
vb@951
|
1696 |
NULL);
|
roker@529
|
1697 |
assert(int_result == SQLITE_OK);
|
vb@62
|
1698 |
|
krista@2478
|
1699 |
int_result = sqlite3_prepare_v2(_session->db, sql_set_identity_entry,
|
krista@2478
|
1700 |
(int)strlen(sql_set_identity_entry), &_session->set_identity_entry, NULL);
|
krista@2478
|
1701 |
assert(int_result == SQLITE_OK);
|
krista@2478
|
1702 |
|
krista@2478
|
1703 |
int_result = sqlite3_prepare_v2(_session->db, sql_update_identity_entry,
|
krista@2478
|
1704 |
(int)strlen(sql_update_identity_entry), &_session->update_identity_entry, NULL);
|
roker@529
|
1705 |
assert(int_result == SQLITE_OK);
|
vb@62
|
1706 |
|
krista@2480
|
1707 |
int_result = sqlite3_prepare_v2(_session->db, sql_exists_identity_entry,
|
krista@2480
|
1708 |
(int)strlen(sql_exists_identity_entry), &_session->exists_identity_entry, NULL);
|
krista@2480
|
1709 |
assert(int_result == SQLITE_OK);
|
krista@2480
|
1710 |
|
vb@932
|
1711 |
int_result = sqlite3_prepare_v2(_session->db, sql_set_identity_flags,
|
vb@951
|
1712 |
(int)strlen(sql_set_identity_flags), &_session->set_identity_flags,
|
vb@951
|
1713 |
NULL);
|
vb@932
|
1714 |
assert(int_result == SQLITE_OK);
|
vb@932
|
1715 |
|
edouard@1394
|
1716 |
int_result = sqlite3_prepare_v2(_session->db, sql_unset_identity_flags,
|
edouard@1394
|
1717 |
(int)strlen(sql_unset_identity_flags), &_session->unset_identity_flags,
|
edouard@1394
|
1718 |
NULL);
|
edouard@1394
|
1719 |
assert(int_result == SQLITE_OK);
|
edouard@1394
|
1720 |
|
krista@3729
|
1721 |
int_result = sqlite3_prepare_v2(_session->db, sql_set_pEp_version,
|
krista@3729
|
1722 |
(int)strlen(sql_set_pEp_version), &_session->set_pEp_version,
|
krista@3729
|
1723 |
NULL);
|
krista@3729
|
1724 |
assert(int_result == SQLITE_OK);
|
krista@3730
|
1725 |
|
krista@3730
|
1726 |
int_result = sqlite3_prepare_v2(_session->db, sql_upgrade_pEp_version_by_user_id,
|
krista@3730
|
1727 |
(int)strlen(sql_upgrade_pEp_version_by_user_id), &_session->upgrade_pEp_version_by_user_id,
|
krista@3730
|
1728 |
NULL);
|
krista@3730
|
1729 |
assert(int_result == SQLITE_OK);
|
krista@3729
|
1730 |
|
vb@0
|
1731 |
int_result = sqlite3_prepare_v2(_session->db, sql_set_trust,
|
Edouard@417
|
1732 |
(int)strlen(sql_set_trust), &_session->set_trust, NULL);
|
roker@529
|
1733 |
assert(int_result == SQLITE_OK);
|
vb@62
|
1734 |
|
krista@2478
|
1735 |
int_result = sqlite3_prepare_v2(_session->db, sql_update_trust,
|
krista@2478
|
1736 |
(int)strlen(sql_update_trust), &_session->update_trust, NULL);
|
krista@2478
|
1737 |
assert(int_result == SQLITE_OK);
|
krista@2478
|
1738 |
|
vb@2834
|
1739 |
int_result = sqlite3_prepare_v2(_session->db, sql_update_trust_to_pEp,
|
vb@2834
|
1740 |
(int)strlen(sql_update_trust_to_pEp), &_session->update_trust_to_pEp, NULL);
|
krista@2543
|
1741 |
assert(int_result == SQLITE_OK);
|
krista@2543
|
1742 |
|
krista@2480
|
1743 |
int_result = sqlite3_prepare_v2(_session->db, sql_exists_trust_entry,
|
krista@2480
|
1744 |
(int)strlen(sql_exists_trust_entry), &_session->exists_trust_entry, NULL);
|
krista@2480
|
1745 |
assert(int_result == SQLITE_OK);
|
krista@2480
|
1746 |
|
krista@1799
|
1747 |
int_result = sqlite3_prepare_v2(_session->db, sql_update_trust_for_fpr,
|
krista@1799
|
1748 |
(int)strlen(sql_update_trust_for_fpr), &_session->update_trust_for_fpr, NULL);
|
krista@1799
|
1749 |
assert(int_result == SQLITE_OK);
|
krista@1799
|
1750 |
|
vb@8
|
1751 |
int_result = sqlite3_prepare_v2(_session->db, sql_get_trust,
|
Edouard@417
|
1752 |
(int)strlen(sql_get_trust), &_session->get_trust, NULL);
|
vb@8
|
1753 |
assert(int_result == SQLITE_OK);
|
vb@0
|
1754 |
|
krista@2967
|
1755 |
int_result = sqlite3_prepare_v2(_session->db, sql_get_trust_by_userid,
|
krista@2967
|
1756 |
(int)strlen(sql_get_trust_by_userid), &_session->get_trust_by_userid, NULL);
|
krista@2967
|
1757 |
assert(int_result == SQLITE_OK);
|
krista@2967
|
1758 |
|
vb@251
|
1759 |
int_result = sqlite3_prepare_v2(_session->db, sql_least_trust,
|
Edouard@417
|
1760 |
(int)strlen(sql_least_trust), &_session->least_trust, NULL);
|
vb@251
|
1761 |
assert(int_result == SQLITE_OK);
|
vb@251
|
1762 |
|
krista@2593
|
1763 |
int_result = sqlite3_prepare_v2(_session->db, sql_mark_as_compromised,
|
krista@2593
|
1764 |
(int)strlen(sql_mark_as_compromised), &_session->mark_compromised,
|
vb@951
|
1765 |
NULL);
|
vb@357
|
1766 |
assert(int_result == SQLITE_OK);
|
vb@357
|
1767 |
|
vb@453
|
1768 |
int_result = sqlite3_prepare_v2(_session->db, sql_crashdump,
|
vb@453
|
1769 |
(int)strlen(sql_crashdump), &_session->crashdump, NULL);
|
vb@453
|
1770 |
assert(int_result == SQLITE_OK);
|
vb@453
|
1771 |
|
vb@458
|
1772 |
int_result = sqlite3_prepare_v2(_session->system_db, sql_languagelist,
|
vb@458
|
1773 |
(int)strlen(sql_languagelist), &_session->languagelist, NULL);
|
roker@529
|
1774 |
assert(int_result == SQLITE_OK);
|
vb@458
|
1775 |
|
vb@458
|
1776 |
int_result = sqlite3_prepare_v2(_session->system_db, sql_i18n_token,
|
vb@458
|
1777 |
(int)strlen(sql_i18n_token), &_session->i18n_token, NULL);
|
roker@529
|
1778 |
assert(int_result == SQLITE_OK);
|
krista@2461
|
1779 |
|
fdik@494
|
1780 |
// blacklist
|
fdik@494
|
1781 |
|
fdik@494
|
1782 |
int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_add,
|
fdik@494
|
1783 |
(int)strlen(sql_blacklist_add), &_session->blacklist_add, NULL);
|
fdik@494
|
1784 |
assert(int_result == SQLITE_OK);
|
fdik@494
|
1785 |
|
fdik@494
|
1786 |
int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_delete,
|
vb@951
|
1787 |
(int)strlen(sql_blacklist_delete), &_session->blacklist_delete,
|
vb@951
|
1788 |
NULL);
|
fdik@494
|
1789 |
assert(int_result == SQLITE_OK);
|
fdik@494
|
1790 |
|
fdik@494
|
1791 |
int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_is_listed,
|
vb@951
|
1792 |
(int)strlen(sql_blacklist_is_listed),
|
vb@951
|
1793 |
&_session->blacklist_is_listed, NULL);
|
fdik@494
|
1794 |
assert(int_result == SQLITE_OK);
|
fdik@494
|
1795 |
|
fdik@494
|
1796 |
int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_retrieve,
|
vb@951
|
1797 |
(int)strlen(sql_blacklist_retrieve), &_session->blacklist_retrieve,
|
vb@951
|
1798 |
NULL);
|
vb@482
|
1799 |
assert(int_result == SQLITE_OK);
|
krista@1275
|
1800 |
|
Edouard@584
|
1801 |
// Own keys
|
Edouard@584
|
1802 |
|
Edouard@584
|
1803 |
int_result = sqlite3_prepare_v2(_session->db, sql_own_key_is_listed,
|
vb@951
|
1804 |
(int)strlen(sql_own_key_is_listed), &_session->own_key_is_listed,
|
vb@951
|
1805 |
NULL);
|
Edouard@584
|
1806 |
assert(int_result == SQLITE_OK);
|
krista@3346
|
1807 |
|
krista@3346
|
1808 |
int_result = sqlite3_prepare_v2(_session->db, sql_is_own_address,
|
krista@3346
|
1809 |
(int)strlen(sql_is_own_address), &_session->is_own_address,
|
krista@3346
|
1810 |
NULL);
|
krista@3346
|
1811 |
assert(int_result == SQLITE_OK);
|
Edouard@584
|
1812 |
|
vb@955
|
1813 |
int_result = sqlite3_prepare_v2(_session->db, sql_own_identities_retrieve,
|
vb@955
|
1814 |
(int)strlen(sql_own_identities_retrieve),
|
vb@955
|
1815 |
&_session->own_identities_retrieve, NULL);
|
Edouard@584
|
1816 |
assert(int_result == SQLITE_OK);
|
vb@633
|
1817 |
|
edouard@1394
|
1818 |
int_result = sqlite3_prepare_v2(_session->db, sql_own_keys_retrieve,
|
edouard@1394
|
1819 |
(int)strlen(sql_own_keys_retrieve),
|
edouard@1394
|
1820 |
&_session->own_keys_retrieve, NULL);
|
edouard@1394
|
1821 |
assert(int_result == SQLITE_OK);
|
edouard@1394
|
1822 |
|
krista@2461
|
1823 |
// int_result = sqlite3_prepare_v2(_session->db, sql_set_own_key,
|
krista@2461
|
1824 |
// (int)strlen(sql_set_own_key),
|
krista@2461
|
1825 |
// &_session->set_own_key, NULL);
|
krista@2461
|
1826 |
// assert(int_result == SQLITE_OK);
|
edouard@1370
|
1827 |
|
vb@633
|
1828 |
// Sequence
|
vb@633
|
1829 |
|
vb@633
|
1830 |
int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value1,
|
vb@951
|
1831 |
(int)strlen(sql_sequence_value1), &_session->sequence_value1,
|
vb@951
|
1832 |
NULL);
|
vb@633
|
1833 |
assert(int_result == SQLITE_OK);
|
vb@633
|
1834 |
|
vb@633
|
1835 |
int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value2,
|
vb@951
|
1836 |
(int)strlen(sql_sequence_value2), &_session->sequence_value2,
|
vb@951
|
1837 |
NULL);
|
vb@633
|
1838 |
assert(int_result == SQLITE_OK);
|
vb@633
|
1839 |
|
Edouard@693
|
1840 |
// Revocation tracking
|
Edouard@693
|
1841 |
|
Edouard@693
|
1842 |
int_result = sqlite3_prepare_v2(_session->db, sql_set_revoked,
|
vb@951
|
1843 |
(int)strlen(sql_set_revoked), &_session->set_revoked, NULL);
|
Edouard@693
|
1844 |
assert(int_result == SQLITE_OK);
|
Edouard@693
|
1845 |
|
Edouard@693
|
1846 |
int_result = sqlite3_prepare_v2(_session->db, sql_get_revoked,
|
vb@951
|
1847 |
(int)strlen(sql_get_revoked), &_session->get_revoked, NULL);
|
Edouard@693
|
1848 |
assert(int_result == SQLITE_OK);
|
Edouard@693
|
1849 |
|
krista@2752
|
1850 |
int_result = sqlite3_prepare_v2(_session->db, sql_get_replacement_fpr,
|
krista@2752
|
1851 |
(int)strlen(sql_get_replacement_fpr), &_session->get_replacement_fpr, NULL);
|
krista@2752
|
1852 |
assert(int_result == SQLITE_OK);
|
krista@2752
|
1853 |
|
krista@2471
|
1854 |
int_result = sqlite3_prepare_v2(_session->db, sql_add_mistrusted_key,
|
krista@2471
|
1855 |
(int)strlen(sql_add_mistrusted_key), &_session->add_mistrusted_key, NULL);
|
krista@2471
|
1856 |
assert(int_result == SQLITE_OK);
|
krista@2471
|
1857 |
|
krista@2471
|
1858 |
int_result = sqlite3_prepare_v2(_session->db, sql_delete_mistrusted_key,
|
krista@2471
|
1859 |
(int)strlen(sql_delete_mistrusted_key), &_session->delete_mistrusted_key, NULL);
|
krista@2471
|
1860 |
assert(int_result == SQLITE_OK);
|
krista@2471
|
1861 |
|
krista@2471
|
1862 |
int_result = sqlite3_prepare_v2(_session->db, sql_is_mistrusted_key,
|
krista@2471
|
1863 |
(int)strlen(sql_is_mistrusted_key), &_session->is_mistrusted_key, NULL);
|
krista@2471
|
1864 |
assert(int_result == SQLITE_OK);
|
krista@2471
|
1865 |
|
vb@65
|
1866 |
status = init_cryptotech(_session, in_first);
|
vb@65
|
1867 |
if (status != PEP_STATUS_OK)
|
vb@2834
|
1868 |
goto pEp_error;
|
vb@65
|
1869 |
|
vb@65
|
1870 |
status = init_transport_system(_session, in_first);
|
vb@65
|
1871 |
if (status != PEP_STATUS_OK)
|
vb@2834
|
1872 |
goto pEp_error;
|
vb@0
|
1873 |
|
vb@65
|
1874 |
status = log_event(_session, "init", "pEp " PEP_ENGINE_VERSION, NULL, NULL);
|
vb@65
|
1875 |
if (status != PEP_STATUS_OK)
|
vb@2834
|
1876 |
goto pEp_error;
|
vb@65
|
1877 |
|
vb@464
|
1878 |
// runtime config
|
vb@464
|
1879 |
|
edouard@1752
|
1880 |
if (very_first)
|
edouard@1752
|
1881 |
{
|
vb@2539
|
1882 |
#ifdef USE_GPG
|
edouard@1752
|
1883 |
// On first run, all private keys already present in PGP keyring
|
edouard@1752
|
1884 |
// are taken as own in order to seamlessly integrate with
|
edouard@1752
|
1885 |
// pre-existing GPG setup.
|
edouard@1752
|
1886 |
|
krista@2458
|
1887 |
// Note: earlier fears about danger because of DB reinitialisation should
|
krista@2458
|
1888 |
// be a non-issue here, as we ONLY take the ultimately trusted keys now.
|
krista@2458
|
1889 |
// Thus, unless the user has assigned ultimate trust through PGP, there is
|
krista@2458
|
1890 |
// no chance of automatically imported pEp keys from a previous run making
|
krista@2458
|
1891 |
// their way into PEP trusted status without explicit action (Bare imported
|
krista@2458
|
1892 |
// private keys have an 'unknown' trust designation in PGP).
|
krista@2458
|
1893 |
|
krista@2458
|
1894 |
// We don't really worry about the status here.
|
vb@2539
|
1895 |
status = pgp_import_ultimately_trusted_keypairs(_session);
|
vb@2539
|
1896 |
#endif // USE_GPG
|
edouard@1752
|
1897 |
}
|
vb@464
|
1898 |
|
roker@529
|
1899 |
*session = _session;
|
krista@2176
|
1900 |
|
krista@2176
|
1901 |
// Note: Following statement is NOT for any cryptographic/secure functionality; it is
|
krista@2176
|
1902 |
// ONLY used for some randomness in generated outer message ID, which are
|
krista@2176
|
1903 |
// required by the RFC to be globally unique!
|
vb@2571
|
1904 |
srand((unsigned int) time(NULL));
|
krista@2176
|
1905 |
|
roker@529
|
1906 |
return PEP_STATUS_OK;
|
vb@62
|
1907 |
|
vb@65
|
1908 |
enomem:
|
vb@65
|
1909 |
status = PEP_OUT_OF_MEMORY;
|
vb@65
|
1910 |
|
vb@2834
|
1911 |
pEp_error:
|
vb@65
|
1912 |
release(_session);
|
vb@65
|
1913 |
return status;
|
vb@0
|
1914 |
}
|
vb@0
|
1915 |
|
vb@0
|
1916 |
DYNAMIC_API void release(PEP_SESSION session)
|
vb@0
|
1917 |
{
|
vb@62
|
1918 |
bool out_last = false;
|
krista@2124
|
1919 |
int _count = --init_count;
|
edouard@2112
|
1920 |
|
edouard@2112
|
1921 |
assert(_count >= -1);
|
roker@529
|
1922 |
assert(session);
|
vb@0
|
1923 |
|
edouard@2112
|
1924 |
if (!((_count >= -1) && session))
|
vb@191
|
1925 |
return;
|
vb@191
|
1926 |
|
vb@62
|
1927 |
// a small race condition but still a race condition
|
vb@113
|
1928 |
// mitigated by calling caveat (see documentation)
|
krista@2125
|
1929 |
// (release() is to be guarded by a mutex by the caller)
|
edouard@2112
|
1930 |
if (_count == -1)
|
vb@62
|
1931 |
out_last = true;
|
vb@62
|
1932 |
|
roker@529
|
1933 |
if (session) {
|
vb@3391
|
1934 |
free_Sync_state(session);
|
vb@986
|
1935 |
|
roker@529
|
1936 |
if (session->db) {
|
vb@65
|
1937 |
if (session->log)
|
vb@65
|
1938 |
sqlite3_finalize(session->log);
|
vb@517
|
1939 |
if (session->trustword)
|
vb@517
|
1940 |
sqlite3_finalize(session->trustword);
|
vb@65
|
1941 |
if (session->get_identity)
|
vb@65
|
1942 |
sqlite3_finalize(session->get_identity);
|
krista@2461
|
1943 |
if (session->get_identity_without_trust_check)
|
krista@2461
|
1944 |
sqlite3_finalize(session->get_identity_without_trust_check);
|
krista@2461
|
1945 |
if (session->get_identities_by_address)
|
krista@2461
|
1946 |
sqlite3_finalize(session->get_identities_by_address);
|
krista@2966
|
1947 |
if (session->get_identities_by_userid)
|
krista@2893
|
1948 |
sqlite3_finalize(session->get_identities_by_userid);
|
krista@2893
|
1949 |
if (session->get_identities_by_main_key_id)
|
krista@2893
|
1950 |
sqlite3_finalize(session->get_identities_by_main_key_id);
|
krista@2461
|
1951 |
if (session->get_user_default_key)
|
krista@2893
|
1952 |
sqlite3_finalize(session->get_user_default_key);
|
krista@2893
|
1953 |
if (session->get_all_keys_for_user)
|
krista@2893
|
1954 |
sqlite3_finalize(session->get_all_keys_for_user);
|
krista@2461
|
1955 |
if (session->get_default_own_userid)
|
krista@2461
|
1956 |
sqlite3_finalize(session->get_default_own_userid);
|
krista@2461
|
1957 |
if (session->get_userid_alias_default)
|
krista@2461
|
1958 |
sqlite3_finalize(session->get_userid_alias_default);
|
krista@2461
|
1959 |
if (session->add_userid_alias)
|
krista@2461
|
1960 |
sqlite3_finalize(session->add_userid_alias);
|
krista@1799
|
1961 |
if (session->replace_identities_fpr)
|
krista@1799
|
1962 |
sqlite3_finalize(session->replace_identities_fpr);
|
krista@2461
|
1963 |
if (session->remove_fpr_as_default)
|
krista@2461
|
1964 |
sqlite3_finalize(session->remove_fpr_as_default);
|
vb@65
|
1965 |
if (session->set_person)
|
vb@65
|
1966 |
sqlite3_finalize(session->set_person);
|
krista@2965
|
1967 |
if (session->delete_person)
|
krista@2965
|
1968 |
sqlite3_finalize(session->delete_person);
|
vb@2833
|
1969 |
if (session->set_as_pEp_user)
|
vb@2833
|
1970 |
sqlite3_finalize(session->set_as_pEp_user);
|
krista@3730
|
1971 |
if (session->upgrade_pEp_version_by_user_id)
|
krista@3730
|
1972 |
sqlite3_finalize(session->upgrade_pEp_version_by_user_id);
|
vb@2833
|
1973 |
if (session->is_pEp_user)
|
vb@2833
|
1974 |
sqlite3_finalize(session->is_pEp_user);
|
krista@2468
|
1975 |
if (session->exists_person)
|
krista@2747
|
1976 |
sqlite3_finalize(session->exists_person);
|
krista@2747
|
1977 |
if (session->add_into_social_graph)
|
krista@2752
|
1978 |
sqlite3_finalize(session->add_into_social_graph);
|
krista@2752
|
1979 |
if (session->get_own_address_binding_from_contact)
|
krista@2752
|
1980 |
sqlite3_finalize(session->get_own_address_binding_from_contact);
|
krista@2752
|
1981 |
if (session->set_revoke_contact_as_notified)
|
krista@2752
|
1982 |
sqlite3_finalize(session->set_revoke_contact_as_notified);
|
krista@2752
|
1983 |
if (session->get_contacted_ids_from_revoke_fpr)
|
krista@2752
|
1984 |
sqlite3_finalize(session->get_contacted_ids_from_revoke_fpr);
|
krista@2752
|
1985 |
if (session->was_id_for_revoke_contacted)
|
krista@2800
|
1986 |
sqlite3_finalize(session->was_id_for_revoke_contacted);
|
krista@2800
|
1987 |
if (session->get_last_contacted)
|
krista@2800
|
1988 |
sqlite3_finalize(session->get_last_contacted);
|
vb@65
|
1989 |
if (session->set_pgp_keypair)
|
vb@65
|
1990 |
sqlite3_finalize(session->set_pgp_keypair);
|
krista@2480
|
1991 |
if (session->exists_identity_entry)
|
krista@2480
|
1992 |
sqlite3_finalize(session->exists_identity_entry);
|
krista@2478
|
1993 |
if (session->set_identity_entry)
|
krista@2478
|
1994 |
sqlite3_finalize(session->set_identity_entry);
|
krista@2478
|
1995 |
if (session->update_identity_entry)
|
krista@2478
|
1996 |
sqlite3_finalize(session->update_identity_entry);
|
vb@932
|
1997 |
if (session->set_identity_flags)
|
vb@932
|
1998 |
sqlite3_finalize(session->set_identity_flags);
|
edouard@1394
|
1999 |
if (session->unset_identity_flags)
|
edouard@1394
|
2000 |
sqlite3_finalize(session->unset_identity_flags);
|
krista@3730
|
2001 |
if (session->set_pEp_version)
|
krista@3730
|
2002 |
sqlite3_finalize(session->set_pEp_version);
|
krista@2480
|
2003 |
if (session->exists_trust_entry)
|
krista@2480
|
2004 |
sqlite3_finalize(session->exists_trust_entry);
|
vb@65
|
2005 |
if (session->set_trust)
|
vb@65
|
2006 |
sqlite3_finalize(session->set_trust);
|
krista@2478
|
2007 |
if (session->update_trust)
|
krista@2543
|
2008 |
sqlite3_finalize(session->update_trust);
|
vb@2834
|
2009 |
if (session->update_trust_to_pEp)
|
vb@2834
|
2010 |
sqlite3_finalize(session->update_trust_to_pEp);
|
krista@1799
|
2011 |
if (session->update_trust_for_fpr)
|
krista@1799
|
2012 |
sqlite3_finalize(session->update_trust_for_fpr);
|
vb@65
|
2013 |
if (session->get_trust)
|
vb@65
|
2014 |
sqlite3_finalize(session->get_trust);
|
krista@2967
|
2015 |
if (session->get_trust_by_userid)
|
krista@2967
|
2016 |
sqlite3_finalize(session->get_trust_by_userid);
|
vb@251
|
2017 |
if (session->least_trust)
|
vb@251
|
2018 |
sqlite3_finalize(session->least_trust);
|
krista@2593
|
2019 |
if (session->mark_compromised)
|
krista@2593
|
2020 |
sqlite3_finalize(session->mark_compromised);
|
vb@517
|
2021 |
if (session->crashdump)
|
vb@517
|
2022 |
sqlite3_finalize(session->crashdump);
|
vb@517
|
2023 |
if (session->languagelist)
|
vb@517
|
2024 |
sqlite3_finalize(session->languagelist);
|
vb@517
|
2025 |
if (session->i18n_token)
|
vb@517
|
2026 |
sqlite3_finalize(session->i18n_token);
|
krista@2461
|
2027 |
if (session->replace_userid)
|
krista@2461
|
2028 |
sqlite3_finalize(session->replace_userid);
|
krista@2948
|
2029 |
if (session->delete_key)
|
krista@2948
|
2030 |
sqlite3_finalize(session->delete_key);
|
krista@2461
|
2031 |
if (session->replace_main_user_fpr)
|
krista@2461
|
2032 |
sqlite3_finalize(session->replace_main_user_fpr);
|
krista@2461
|
2033 |
if (session->get_main_user_fpr)
|
krista@2461
|
2034 |
sqlite3_finalize(session->get_main_user_fpr);
|
krista@2461
|
2035 |
if (session->refresh_userid_default_key)
|
krista@2461
|
2036 |
sqlite3_finalize(session->refresh_userid_default_key);
|
vb@517
|
2037 |
if (session->blacklist_add)
|
vb@517
|
2038 |
sqlite3_finalize(session->blacklist_add);
|
vb@517
|
2039 |
if (session->blacklist_delete)
|
vb@517
|
2040 |
sqlite3_finalize(session->blacklist_delete);
|
vb@517
|
2041 |
if (session->blacklist_is_listed)
|
vb@517
|
2042 |
sqlite3_finalize(session->blacklist_is_listed);
|
vb@517
|
2043 |
if (session->blacklist_retrieve)
|
vb@517
|
2044 |
sqlite3_finalize(session->blacklist_retrieve);
|
krista@957
|
2045 |
if (session->own_key_is_listed)
|
krista@957
|
2046 |
sqlite3_finalize(session->own_key_is_listed);
|
krista@3346
|
2047 |
if (session->is_own_address)
|
krista@3346
|
2048 |
sqlite3_finalize(session->is_own_address);
|
roker@1002
|
2049 |
if (session->own_identities_retrieve)
|
roker@1002
|
2050 |
sqlite3_finalize(session->own_identities_retrieve);
|
edouard@1394
|
2051 |
if (session->own_keys_retrieve)
|
edouard@1394
|
2052 |
sqlite3_finalize(session->own_keys_retrieve);
|
krista@2461
|
2053 |
// if (session->set_own_key)
|
krista@2461
|
2054 |
// sqlite3_finalize(session->set_own_key);
|
krista@957
|
2055 |
if (session->sequence_value1)
|
krista@957
|
2056 |
sqlite3_finalize(session->sequence_value1);
|
krista@957
|
2057 |
if (session->sequence_value2)
|
krista@960
|
2058 |
sqlite3_finalize(session->sequence_value2);
|
krista@957
|
2059 |
if (session->set_revoked)
|
krista@957
|
2060 |
sqlite3_finalize(session->set_revoked);
|
krista@957
|
2061 |
if (session->get_revoked)
|
krista@957
|
2062 |
sqlite3_finalize(session->get_revoked);
|
krista@2752
|
2063 |
if (session->get_replacement_fpr)
|
krista@2752
|
2064 |
sqlite3_finalize(session->get_replacement_fpr);
|
krista@2471
|
2065 |
if (session->add_mistrusted_key)
|
krista@2471
|
2066 |
sqlite3_finalize(session->add_mistrusted_key);
|
krista@2471
|
2067 |
if (session->delete_mistrusted_key)
|
krista@2471
|
2068 |
sqlite3_finalize(session->delete_mistrusted_key);
|
krista@2471
|
2069 |
if (session->is_mistrusted_key)
|
krista@2471
|
2070 |
sqlite3_finalize(session->is_mistrusted_key);
|
krista@2526
|
2071 |
|
krista@2526
|
2072 |
if (session->db) {
|
krista@2526
|
2073 |
sqlite3_exec(
|
krista@2526
|
2074 |
session->db,
|
krista@2526
|
2075 |
"PRAGMA optimize;\n",
|
krista@2526
|
2076 |
NULL,
|
krista@2526
|
2077 |
NULL,
|
krista@2526
|
2078 |
NULL
|
krista@2526
|
2079 |
);
|
vb@65
|
2080 |
sqlite3_close_v2(session->db);
|
krista@2526
|
2081 |
}
|
vb@65
|
2082 |
if (session->system_db)
|
vb@65
|
2083 |
sqlite3_close_v2(session->system_db);
|
roker@529
|
2084 |
}
|
vb@28
|
2085 |
|
vb@65
|
2086 |
release_transport_system(session, out_last);
|
vb@65
|
2087 |
release_cryptotech(session, out_last);
|
vb@62
|
2088 |
|
roker@1722
|
2089 |
#ifdef DEBUG_ERRORSTACK
|
roker@1722
|
2090 |
free_stringlist(session->errorstack);
|
roker@1722
|
2091 |
#endif
|
vb@65
|
2092 |
free(session);
|
vb@65
|
2093 |
}
|
vb@0
|
2094 |
}
|
vb@0
|
2095 |
|
vb@467
|
2096 |
DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable)
|
vb@464
|
2097 |
{
|
vb@464
|
2098 |
assert(session);
|
vb@3691
|
2099 |
if (session)
|
vb@3691
|
2100 |
session->passive_mode = enable;
|
vb@464
|
2101 |
}
|
vb@464
|
2102 |
|
vb@467
|
2103 |
DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable)
|
vb@464
|
2104 |
{
|
vb@464
|
2105 |
assert(session);
|
vb@3691
|
2106 |
if (session)
|
vb@3691
|
2107 |
session->unencrypted_subject = enable;
|
vb@464
|
2108 |
}
|
vb@464
|
2109 |
|
vb@1819
|
2110 |
DYNAMIC_API void config_service_log(PEP_SESSION session, bool enable)
|
vb@1819
|
2111 |
{
|
vb@1819
|
2112 |
assert(session);
|
vb@3691
|
2113 |
if (session)
|
vb@3691
|
2114 |
session->service_log = enable;
|
vb@1819
|
2115 |
}
|
vb@1819
|
2116 |
|
vb@0
|
2117 |
DYNAMIC_API PEP_STATUS log_event(
|
vb@450
|
2118 |
PEP_SESSION session,
|
vb@451
|
2119 |
const char *title,
|
vb@451
|
2120 |
const char *entity,
|
vb@451
|
2121 |
const char *description,
|
vb@451
|
2122 |
const char *comment
|
vb@0
|
2123 |
)
|
vb@0
|
2124 |
{
|
krista@3436
|
2125 |
|
krista@3436
|
2126 |
// N.B. If testing (so NDEBUG not defined) but this message is spam,
|
krista@3436
|
2127 |
// put -D_PEP_SERVICE_LOG_OFF into CFLAGS/CXXFLAGS
|
krista@3436
|
2128 |
#if !defined(NDEBUG) && !defined(_PEP_SERVICE_LOG_OFF)
|
vb@3367
|
2129 |
fprintf(stdout, "\n*** %s %s %s %s\n", title, entity, description, comment);
|
vb@3367
|
2130 |
session->service_log = true;
|
vb@3367
|
2131 |
#endif
|
vb@3367
|
2132 |
|
vb@3367
|
2133 |
// PEP_STATUS status = PEP_STATUS_OK;
|
krista@2518
|
2134 |
// int result;
|
krista@2518
|
2135 |
//
|
krista@2518
|
2136 |
// assert(session);
|
krista@2518
|
2137 |
// assert(title);
|
krista@2518
|
2138 |
// assert(entity);
|
krista@2518
|
2139 |
//
|
krista@2518
|
2140 |
// if (!(session && title && entity))
|
krista@2518
|
2141 |
// return PEP_ILLEGAL_VALUE;
|
krista@2518
|
2142 |
//
|
krista@2518
|
2143 |
// sqlite3_reset(session->log);
|
krista@2518
|
2144 |
// sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
|
krista@2518
|
2145 |
// sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
|
krista@2518
|
2146 |
// if (description)
|
krista@2518
|
2147 |
// sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
|
krista@2518
|
2148 |
// else
|
krista@2518
|
2149 |
// sqlite3_bind_null(session->log, 3);
|
krista@2518
|
2150 |
// if (comment)
|
krista@2518
|
2151 |
// sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
|
krista@2518
|
2152 |
// else
|
krista@2518
|
2153 |
// sqlite3_bind_null(session->log, 4);
|
krista@2518
|
2154 |
// result = sqlite3_step(session->log);
|
krista@2518
|
2155 |
// sqlite3_reset(session->log);
|
krista@2518
|
2156 |
//
|
krista@2499
|
2157 |
return PEP_STATUS_OK; // We ignore errors for this function.
|
vb@0
|
2158 |
}
|
vb@0
|
2159 |
|
vb@1819
|
2160 |
DYNAMIC_API PEP_STATUS log_service(
|
vb@1819
|
2161 |
PEP_SESSION session,
|
vb@1819
|
2162 |
const char *title,
|
vb@1819
|
2163 |
const char *entity,
|
vb@1819
|
2164 |
const char *description,
|
vb@1819
|
2165 |
const char *comment
|
vb@1819
|
2166 |
)
|
vb@1819
|
2167 |
{
|
vb@1819
|
2168 |
assert(session);
|
vb@1819
|
2169 |
if (!session)
|
vb@1819
|
2170 |
return PEP_ILLEGAL_VALUE;
|
vb@1819
|
2171 |
|
vb@1819
|
2172 |
if (session->service_log)
|
vb@1819
|
2173 |
return log_event(session, title, entity, description, comment);
|
vb@1819
|
2174 |
else
|
vb@1819
|
2175 |
return PEP_STATUS_OK;
|
vb@1819
|
2176 |
}
|
vb@1819
|
2177 |
|
vb@233
|
2178 |
DYNAMIC_API PEP_STATUS trustword(
|
vb@0
|
2179 |
PEP_SESSION session, uint16_t value, const char *lang,
|
vb@0
|
2180 |
char **word, size_t *wsize
|
vb@0
|
2181 |
)
|
vb@0
|
2182 |
{
|
roker@529
|
2183 |
PEP_STATUS status = PEP_STATUS_OK;
|
vb@0
|
2184 |
|
roker@529
|
2185 |
assert(session);
|
roker@529
|
2186 |
assert(word);
|
roker@529
|
2187 |
assert(wsize);
|
vb@0
|
2188 |
|
vb@191
|
2189 |
if (!(session && word && wsize))
|
vb@191
|
2190 |
return PEP_ILLEGAL_VALUE;
|
vb@191
|
2191 |
|
roker@529
|
2192 |
*word = NULL;
|
roker@529
|
2193 |
*wsize = 0;
|
vb@0
|
2194 |
|
roker@529
|
2195 |
if (lang == NULL)
|
roker@529
|
2196 |
lang = "en";
|
vb@0
|
2197 |
|
roker@529
|
2198 |
assert((lang[0] >= 'A' && lang[0] <= 'Z')
|
vb@0
|
2199 |
|| (lang[0] >= 'a' && lang[0] <= 'z'));
|
roker@529
|
2200 |
assert((lang[1] >= 'A' && lang[1] <= 'Z')
|
vb@0
|
2201 |
|| (lang[1] >= 'a' && lang[1] <= 'z'));
|
roker@529
|
2202 |
assert(lang[2] == 0);
|
vb@0
|
2203 |
|
roker@529
|
2204 |
sqlite3_reset(session->trustword);
|
vb@233
|
2205 |
sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
|
roker@529
|
2206 |
sqlite3_bind_int(session->trustword, 2, value);
|
vb@0
|
2207 |
|
roker@880
|
2208 |
const int result = sqlite3_step(session->trustword);
|
roker@529
|
2209 |
if (result == SQLITE_ROW) {
|
vb@233
|
2210 |
*word = strdup((const char *) sqlite3_column_text(session->trustword,
|
vb@0
|
2211 |
1));
|
roker@529
|
2212 |
if (*word)
|
vb@233
|
2213 |
*wsize = sqlite3_column_bytes(session->trustword, 1);
|
roker@529
|
2214 |
else
|
Edouard@693
|
2215 |
status = PEP_OUT_OF_MEMORY;
|
roker@529
|
2216 |
} else
|
roker@529
|
2217 |
status = PEP_TRUSTWORD_NOT_FOUND;
|
vb@0
|
2218 |
|
roker@529
|
2219 |
sqlite3_reset(session->trustword);
|
roker@529
|
2220 |
return status;
|
vb@0
|
2221 |
}
|
vb@0
|
2222 |
|
vb@233
|
2223 |
DYNAMIC_API PEP_STATUS trustwords(
|
vb@0
|
2224 |
PEP_SESSION session, const char *fingerprint, const char *lang,
|
vb@0
|
2225 |
char **words, size_t *wsize, int max_words
|
vb@0
|
2226 |
)
|
vb@0
|
2227 |
{
|
roker@529
|
2228 |
const char *source = fingerprint;
|
vb@0
|
2229 |
|
roker@529
|
2230 |
assert(session);
|
roker@529
|
2231 |
assert(fingerprint);
|
roker@529
|
2232 |
assert(words);
|
roker@529
|
2233 |
assert(wsize);
|
roker@529
|
2234 |
assert(max_words >= 0);
|
vb@0
|
2235 |
|
vb@191
|
2236 |
if (!(session && fingerprint && words && wsize && max_words >= 0))
|
vb@191
|
2237 |
return PEP_ILLEGAL_VALUE;
|
vb@191
|
2238 |
|
roker@529
|
2239 |
*words = NULL;
|
roker@529
|
2240 |
*wsize = 0;
|
vb@0
|
2241 |
|
roker@1559
|
2242 |
char *buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
|
vb@0
|
2243 |
assert(buffer);
|
vb@0
|
2244 |
if (buffer == NULL)
|
vb@0
|
2245 |
return PEP_OUT_OF_MEMORY;
|
roker@1559
|
2246 |
char *dest = buffer;
|
vb@0
|
2247 |
|
roker@1559
|
2248 |
const size_t fsize = strlen(fingerprint);
|
vb@0
|
2249 |
|
roker@529
|
2250 |
if (!lang || !lang[0])
|
roker@529
|
2251 |
lang = "en";
|
vb@0
|
2252 |
|
roker@529
|
2253 |
assert((lang[0] >= 'A' && lang[0] <= 'Z')
|
vb@0
|
2254 |
|| (lang[0] >= 'a' && lang[0] <= 'z'));
|
roker@529
|
2255 |
assert((lang[1] >= 'A' && lang[1] <= 'Z')
|
vb@0
|
2256 |
|| (lang[1] >= 'a' && lang[1] <= 'z'));
|
roker@529
|
2257 |
assert(lang[2] == 0);
|
vb@0
|
2258 |
|
roker@529
|
2259 |
int n_words = 0;
|
roker@529
|
2260 |
while (source < fingerprint + fsize) {
|
vb@939
|
2261 |
PEP_STATUS _status;
|
roker@529
|
2262 |
uint16_t value;
|
roker@1559
|
2263 |
char *word = NULL;
|
roker@1559
|
2264 |
size_t _wsize = 0;
|
roker@529
|
2265 |
int j;
|
vb@0
|
2266 |
|
vb@0
|
2267 |
for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
|
roker@529
|
2268 |
if (*source >= 'a' && *source <= 'f')
|
roker@529
|
2269 |
value += (*source - 'a' + 10) << (3 - j++) * 4;
|
roker@529
|
2270 |
else if (*source >= 'A' && *source <= 'F')
|
roker@529
|
2271 |
value += (*source - 'A' + 10) << (3 - j++) * 4;
|
roker@529
|
2272 |
else if (*source >= '0' && *source <= '9')
|
roker@529
|
2273 |
value += (*source - '0') << (3 - j++) * 4;
|
roker@529
|
2274 |
|
roker@529
|
2275 |
source++;
|
roker@529
|
2276 |
}
|
vb@0
|
2277 |
|
roker@529
|
2278 |
_status = trustword(session, value, lang, &word, &_wsize);
|
vb@0
|
2279 |
if (_status == PEP_OUT_OF_MEMORY) {
|
vb@0
|
2280 |
free(buffer);
|
vb@0
|
2281 |
return PEP_OUT_OF_MEMORY;
|
vb@0
|
2282 |
}
|
roker@529
|
2283 |
if (word == NULL) {
|
vb@0
|
2284 |
free(buffer);
|
roker@529
|
2285 |
return PEP_TRUSTWORD_NOT_FOUND;
|
vb@0
|
2286 |
}
|
vb@0
|
2287 |
|
roker@529
|
2288 |
if (dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1) {
|
roker@529
|
2289 |
strncpy(dest, word, _wsize);
|
vb@0
|
2290 |
free(word);
|
roker@529
|
2291 |
dest += _wsize;
|
roker@529
|
2292 |
}
|
roker@529
|
2293 |
else {
|
vb@0
|
2294 |
free(word);
|
roker@529
|
2295 |
break; // buffer full
|
vb@0
|
2296 |
}
|
vb@0
|
2297 |
|
roker@529
|
2298 |
if (source < fingerprint + fsize
|
vb@251
|
2299 |
&& dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1)
|
roker@529
|
2300 |
*dest++ = ' ';
|
vb@0
|
2301 |
|
roker@529
|
2302 |
++n_words;
|
roker@529
|
2303 |
if (max_words && n_words >= max_words)
|
roker@529
|
2304 |
break;
|
roker@529
|
2305 |
}
|
vb@0
|
2306 |
|
roker@529
|
2307 |
*words = buffer;
|
roker@529
|
2308 |
*wsize = dest - buffer;
|
roker@529
|
2309 |
return PEP_STATUS_OK;
|
vb@0
|
2310 |
}
|
vb@0
|
2311 |
|
vb@0
|
2312 |
pEp_identity *new_identity(
|
vb@0
|
2313 |
const char *address, const char *fpr, const char *user_id,
|
vb@0
|
2314 |
const char *username
|
vb@0
|
2315 |
)
|
vb@0
|
2316 |
{
|
vb@0
|
2317 |
pEp_identity *result = calloc(1, sizeof(pEp_identity));
|
|