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