NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
private.h File Reference

Shared constants/structs that are private to Autocrypt. More...

#include <sqlite3.h>
#include <stdbool.h>
#include "mutt/lib.h"
+ Include dependency graph for private.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  AccountEntry
 An entry in the Autocrypt account Menu. More...
 

Enumerations

enum  ExpandoDataAutocrypt {
  ED_AUT_ENABLED = 1 , ED_AUT_KEYID , ED_AUT_ADDRESS , ED_AUT_NUMBER ,
  ED_AUT_PREFER_ENCRYPT
}
 Expando UIDs for Autocrypt. More...
 

Functions

 ARRAY_HEAD (AccountEntryArray, struct AccountEntry *)
 
int mutt_autocrypt_account_init (bool prompt)
 Create a new Autocrypt account.
 
void mutt_autocrypt_scan_mailboxes (void)
 Scan mailboxes for Autocrypt headers.
 
int mutt_autocrypt_db_account_delete (struct AutocryptAccount *acct)
 Delete an Account from the Autocrypt database.
 
void mutt_autocrypt_db_account_free (struct AutocryptAccount **ptr)
 Free an AutocryptAccount.
 
int mutt_autocrypt_db_account_get (struct Address *addr, struct AutocryptAccount **account)
 Get Autocrypt Account data from the database.
 
int mutt_autocrypt_db_account_get_all (struct AutocryptAccountArray *aaa)
 Get all accounts from an Autocrypt database.
 
int mutt_autocrypt_db_account_insert (struct Address *addr, const char *keyid, const char *keydata, bool prefer_encrypt)
 Insert an Account into the Autocrypt database.
 
struct AutocryptAccountmutt_autocrypt_db_account_new (void)
 Create a new AutocryptAccount.
 
int mutt_autocrypt_db_account_update (struct AutocryptAccount *acct)
 Update Account info in the Autocrypt database.
 
void mutt_autocrypt_db_close (void)
 Close the Autocrypt SQLite database connection.
 
void mutt_autocrypt_db_gossip_history_free (struct AutocryptGossipHistory **ptr)
 Free an AutocryptGossipHistory.
 
int mutt_autocrypt_db_gossip_history_insert (struct Address *addr, struct AutocryptGossipHistory *gossip_hist)
 Insert a gossip history into the Autocrypt database.
 
struct AutocryptGossipHistorymutt_autocrypt_db_gossip_history_new (void)
 Create a new AutocryptGossipHistory.
 
int mutt_autocrypt_db_init (bool can_create)
 Initialise the Autocrypt SQLite database.
 
void mutt_autocrypt_db_normalize_addr (struct Address *a)
 Normalise an Email Address.
 
void mutt_autocrypt_db_normalize_addrlist (struct AddressList *al)
 Normalise a list of Email Addresses.
 
void mutt_autocrypt_db_peer_free (struct AutocryptPeer **ptr)
 Free an AutocryptPeer.
 
int mutt_autocrypt_db_peer_get (struct Address *addr, struct AutocryptPeer **peer)
 Get peer info from the Autocrypt database.
 
void mutt_autocrypt_db_peer_history_free (struct AutocryptPeerHistory **ptr)
 Free an AutocryptPeerHistory.
 
int mutt_autocrypt_db_peer_history_insert (struct Address *addr, struct AutocryptPeerHistory *peerhist)
 Insert peer history into the Autocrypt database.
 
struct AutocryptPeerHistorymutt_autocrypt_db_peer_history_new (void)
 Create a new AutocryptPeerHistory.
 
int mutt_autocrypt_db_peer_insert (struct Address *addr, struct AutocryptPeer *peer)
 Insert a peer into the Autocrypt database.
 
struct AutocryptPeermutt_autocrypt_db_peer_new (void)
 Create a new AutocryptPeer.
 
int mutt_autocrypt_db_peer_update (struct AutocryptPeer *peer)
 Update the peer info in an Autocrypt database.
 
int mutt_autocrypt_schema_init (void)
 Set up an Autocrypt database.
 
int mutt_autocrypt_schema_update (void)
 Update the version number of the Autocrypt database schema.
 
int mutt_autocrypt_gpgme_create_key (struct Address *addr, struct Buffer *keyid, struct Buffer *keydata)
 Create a GPGME key.
 
int mutt_autocrypt_gpgme_import_key (const char *keydata, struct Buffer *keyid)
 Read a key from GPGME.
 
int mutt_autocrypt_gpgme_init (void)
 Initialise GPGME.
 
bool mutt_autocrypt_gpgme_is_valid_key (const char *keyid)
 Is a key id valid?
 
int mutt_autocrypt_gpgme_select_key (struct Buffer *keyid, struct Buffer *keydata)
 Select a Autocrypt key.
 
int mutt_autocrypt_gpgme_select_or_create_key (struct Address *addr, struct Buffer *keyid, struct Buffer *keydata)
 Ask the user to select or create an Autocrypt key.
 
bool populate_menu (struct Menu *menu)
 Add the Autocrypt data to a Menu.
 

Variables

sqlite3 * AutocryptDB
 Handle to the open Autocrypt database.
 

Detailed Description

Shared constants/structs that are private to Autocrypt.

Authors
  • Kevin J. McCarthy
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file private.h.

Enumeration Type Documentation

◆ ExpandoDataAutocrypt

Expando UIDs for Autocrypt.

See also
ED_AUTOCRYPT, ExpandoDomain
Enumerator
ED_AUT_ENABLED 

AutocryptAccount.enabled.

ED_AUT_KEYID 

AutocryptAccount.keyid.

ED_AUT_ADDRESS 

AccountEntry.addr.

ED_AUT_NUMBER 

AccountEntry.num.

ED_AUT_PREFER_ENCRYPT 

AutocryptAccount.prefer_encrypt.

Definition at line 59 of file private.h.

60{
61 ED_AUT_ENABLED = 1,
66};
@ ED_AUT_ADDRESS
AccountEntry.addr.
Definition: private.h:63
@ ED_AUT_KEYID
AutocryptAccount.keyid.
Definition: private.h:62
@ ED_AUT_NUMBER
AccountEntry.num.
Definition: private.h:64
@ ED_AUT_ENABLED
AutocryptAccount.enabled.
Definition: private.h:61
@ ED_AUT_PREFER_ENCRYPT
AutocryptAccount.prefer_encrypt.
Definition: private.h:65

Function Documentation

◆ ARRAY_HEAD()

ARRAY_HEAD ( AccountEntryArray  ,
struct AccountEntry  
)

◆ mutt_autocrypt_account_init()

int mutt_autocrypt_account_init ( bool  prompt)

Create a new Autocrypt account.

Parameters
promptPrompt the user
Return values
0Success
-1Error

This is used the first time autocrypt is initialized, and in the account menu.

Definition at line 143 of file autocrypt.c.

144{
145 struct Address *addr = NULL;
146 struct AutocryptAccount *account = NULL;
147 bool done = false;
148 int rc = -1;
149 bool prefer_encrypt = false;
150
151 if (prompt)
152 {
153 /* L10N: The first time NeoMutt is started with $autocrypt set, it will
154 create $autocrypt_dir and then prompt to create an autocrypt account
155 with this message. */
156 if (query_yesorno_ignore_macro(_("Create an initial autocrypt account?"), MUTT_YES) != MUTT_YES)
157 return 0;
158 }
159
160 struct Buffer *keyid = buf_pool_get();
161 struct Buffer *keydata = buf_pool_get();
162
163 const struct Address *c_from = cs_subset_address(NeoMutt->sub, "from");
164 if (c_from)
165 {
166 addr = mutt_addr_copy(c_from);
167 const char *const c_real_name = cs_subset_string(NeoMutt->sub, "real_name");
168 if (!addr->personal && c_real_name)
169 addr->personal = buf_new(c_real_name);
170 }
171
172 struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
173 mutt_addrlist_append(&al, addr);
174
175 do
176 {
177 /* L10N: Autocrypt is asking for the email address to use for the
178 autocrypt account. This will generate a key and add a record
179 to the database for use in autocrypt operations. */
180 if (mutt_edit_address(&al, _("Autocrypt account address: "), false) != 0)
181 goto cleanup;
182
183 addr = TAILQ_FIRST(&al);
184 if (!addr || !addr->mailbox || TAILQ_NEXT(addr, entries))
185 {
186 /* L10N: Autocrypt prompts for an account email address, and requires
187 a single address. This is shown if they entered something invalid,
188 nothing, or more than one address for some reason. */
189 mutt_error(_("Please enter a single email address"));
190 done = false;
191 }
192 else
193 {
194 done = true;
195 }
196 } while (!done);
197
198 addr = TAILQ_FIRST(&al);
199 if (mutt_autocrypt_db_account_get(addr, &account) < 0)
200 goto cleanup;
201 if (account)
202 {
203 /* L10N: When creating an autocrypt account, this message will be displayed
204 if there is already an account in the database with the email address
205 they just entered. */
206 mutt_error(_("That email address already has an autocrypt account"));
207 goto cleanup;
208 }
209
210 if (mutt_autocrypt_gpgme_select_or_create_key(addr, keyid, keydata))
211 goto cleanup;
212
213 /* L10N: Autocrypt has a setting "prefer-encrypt".
214 When the recommendation algorithm returns "available" and BOTH sender and
215 recipient choose "prefer-encrypt", encryption will be automatically
216 enabled.
217 Otherwise the UI will show encryption is "available" but the user
218 will be required to enable encryption manually. */
219 if (query_yesorno_ignore_macro(_("Prefer encryption?"), MUTT_NO) == MUTT_YES)
220 prefer_encrypt = true;
221
222 if (mutt_autocrypt_db_account_insert(addr, buf_string(keyid), buf_string(keydata), prefer_encrypt))
223 {
224 goto cleanup;
225 }
226
227 rc = 0;
228
229cleanup:
230 if (rc == 0)
231 {
232 /* L10N: Message displayed after an autocrypt account is successfully created. */
233 mutt_message(_("Autocrypt account creation succeeded"));
234 }
235 else
236 {
237 /* L10N: Error message displayed if creating an autocrypt account failed
238 or was aborted by the user. */
239 mutt_error(_("Autocrypt account creation aborted"));
240 }
241
244 buf_pool_release(&keyid);
245 buf_pool_release(&keydata);
246 return rc;
247}
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition: address.c:1460
void mutt_addrlist_append(struct AddressList *al, struct Address *a)
Append an Address to an AddressList.
Definition: address.c:1480
struct Address * mutt_addr_copy(const struct Address *addr)
Copy the real address.
Definition: address.c:745
const struct Address * cs_subset_address(const struct ConfigSubset *sub, const char *name)
Get an Address config item by name.
Definition: config_type.c:272
int mutt_autocrypt_db_account_get(struct Address *addr, struct AutocryptAccount **account)
Get Autocrypt Account data from the database.
Definition: db.c:265
int mutt_autocrypt_db_account_insert(struct Address *addr, const char *keyid, const char *keydata, bool prefer_encrypt)
Insert an Account into the Autocrypt database.
Definition: db.c:327
void mutt_autocrypt_db_account_free(struct AutocryptAccount **ptr)
Free an AutocryptAccount.
Definition: db.c:246
struct Buffer * buf_new(const char *str)
Allocate a new Buffer.
Definition: buffer.c:304
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:291
int mutt_autocrypt_gpgme_select_or_create_key(struct Address *addr, struct Buffer *keyid, struct Buffer *keydata)
Ask the user to select or create an Autocrypt key.
Definition: gpgme.c:279
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_message(...)
Definition: logging2.h:91
#define _(a)
Definition: message.h:28
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
@ MUTT_NO
User answered 'No', or assume 'No'.
Definition: quad.h:38
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition: quad.h:39
enum QuadOption query_yesorno_ignore_macro(const char *prompt, enum QuadOption def)
Ask the user a Yes/No question ignoring the macro buffer.
Definition: question.c:340
#define TAILQ_FIRST(head)
Definition: queue.h:723
#define TAILQ_NEXT(elm, field)
Definition: queue.h:832
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:637
int mutt_edit_address(struct AddressList *al, const char *field, bool expand_aliases)
Edit an email address.
Definition: send.c:191
An email address.
Definition: address.h:36
struct Buffer * personal
Real name of address.
Definition: address.h:37
struct Buffer * mailbox
Mailbox and host address.
Definition: address.h:38
Autocrypt account.
Definition: lib.h:108
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:112
String manipulation buffer.
Definition: buffer.h:36
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_scan_mailboxes()

void mutt_autocrypt_scan_mailboxes ( void  )

Scan mailboxes for Autocrypt headers.

This is invoked during the first autocrypt initialization, to scan one or more mailboxes for autocrypt headers.

Due to the implementation, header-cached headers are not scanned, so this routine just opens up the mailboxes with $header_cache temporarily disabled.

Definition at line 914 of file autocrypt.c.

915{
916#ifdef USE_HCACHE
917 const char *c_header_cache = cs_subset_path(NeoMutt->sub, "header_cache");
918 char *old_hdrcache = mutt_str_dup(c_header_cache);
919 cs_subset_str_native_set(NeoMutt->sub, "header_cache", 0, NULL);
920#endif
921
922 struct Buffer *folderbuf = buf_pool_get();
923
924 /* L10N: The first time autocrypt is enabled, NeoMutt will ask to scan
925 through one or more mailboxes for Autocrypt: headers. Those headers are
926 then captured in the database as peer records and used for encryption.
927 If this is answered yes, they will be prompted for a mailbox. */
928 enum QuadOption scan = query_yesorno_ignore_macro(_("Scan a mailbox for autocrypt headers?"),
929 MUTT_YES);
930 while (scan == MUTT_YES)
931 {
932 struct Mailbox *m_cur = get_current_mailbox();
933 // L10N: The prompt for a mailbox to scan for Autocrypt: headers
934 if ((!mw_enter_fname(_("Scan mailbox"), folderbuf, true, m_cur, false, NULL,
935 NULL, MUTT_SEL_NO_FLAGS)) &&
936 (!buf_is_empty(folderbuf)))
937 {
938 buf_expand_path_regex(folderbuf, false);
939 struct Mailbox *m_ac = mx_path_resolve(buf_string(folderbuf));
940 /* NOTE: I am purposely *not* executing folder hooks here,
941 * as they can do all sorts of things like push into the getch() buffer.
942 * Authentication should be in account-hooks. */
943 if (mx_mbox_open(m_ac, MUTT_READONLY))
944 {
945 mx_mbox_close(m_ac);
946 }
947 buf_reset(folderbuf);
948 }
949
950 /* L10N: This is the second prompt to see if the user would like
951 to scan more than one mailbox for Autocrypt headers.
952 I'm purposely being extra verbose; asking first then prompting
953 for a mailbox. This is because this is a one-time operation
954 and I don't want them to accidentally ctrl-g and abort it. */
955 scan = query_yesorno_ignore_macro(_("Scan another mailbox for autocrypt headers?"), MUTT_YES);
956 }
957
958#ifdef USE_HCACHE
959 cs_subset_str_native_set(NeoMutt->sub, "header_cache", (intptr_t) old_hdrcache, NULL);
960 old_hdrcache = NULL;
961#endif
962 buf_pool_release(&folderbuf);
963}
#define MUTT_SEL_NO_FLAGS
No flags are set.
Definition: lib.h:56
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:76
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:291
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:168
int mw_enter_fname(const char *prompt, struct Buffer *fname, bool mailbox, struct Mailbox *m, bool multiple, char ***files, int *numfiles, SelectFileFlags flags)
Ask the user to select a file -.
Definition: curs_lib.c:236
struct Mailbox * get_current_mailbox(void)
Get the current Mailbox.
Definition: index.c:715
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
void buf_expand_path_regex(struct Buffer *buf, bool regex)
Create the canonical path (with regex char escaping)
Definition: muttlib.c:122
bool mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
Open a mailbox and parse it.
Definition: mx.c:288
struct Mailbox * mx_path_resolve(const char *path)
Get a Mailbox for a path.
Definition: mx.c:1636
enum MxStatus mx_mbox_close(struct Mailbox *m)
Save changes and close mailbox.
Definition: mx.c:598
#define MUTT_READONLY
Open in read-only mode.
Definition: mxapi.h:43
QuadOption
Possible values for a quad-option.
Definition: quad.h:36
A mailbox.
Definition: mailbox.h:79
int cs_subset_str_native_set(const struct ConfigSubset *sub, const char *name, intptr_t value, struct Buffer *err)
Natively set the value of a string config item.
Definition: subset.c:297
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_account_delete()

int mutt_autocrypt_db_account_delete ( struct AutocryptAccount acct)

Delete an Account from the Autocrypt database.

Parameters
acctAccount to delete
Return values
0Success
-1Error

Definition at line 427 of file db.c.

428{
429 int rc = -1;
430
432 {
433 if (sqlite3_prepare_v3(AutocryptDB,
434 "DELETE from account "
435 "WHERE email_addr = ?;",
436 -1, SQLITE_PREPARE_PERSISTENT, &AccountDeleteStmt, NULL) != SQLITE_OK)
437 {
438 goto cleanup;
439 }
440 }
441
442 if (sqlite3_bind_text(AccountDeleteStmt, 1, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
443 goto cleanup;
444
445 if (sqlite3_step(AccountDeleteStmt) != SQLITE_DONE)
446 goto cleanup;
447
448 rc = 0;
449
450cleanup:
451 sqlite3_reset(AccountDeleteStmt);
452 return rc;
453}
sqlite3 * AutocryptDB
Handle to the open Autocrypt database.
Definition: db.c:56
static sqlite3_stmt * AccountDeleteStmt
Delete an autocrypt account.
Definition: db.c:48
char * email_addr
Email address.
Definition: lib.h:109
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_account_free()

void mutt_autocrypt_db_account_free ( struct AutocryptAccount **  ptr)

Free an AutocryptAccount.

Parameters
ptrAccount to free

Definition at line 246 of file db.c.

247{
248 if (!ptr || !*ptr)
249 return;
250
251 struct AutocryptAccount *ac = *ptr;
252 FREE(&ac->email_addr);
253 FREE(&ac->keyid);
254 FREE(&ac->keydata);
255 FREE(ptr);
256}
#define FREE(x)
Definition: memory.h:55
char * keydata
PGP Key data.
Definition: lib.h:111
char * keyid
PGP Key id.
Definition: lib.h:110
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_account_get()

int mutt_autocrypt_db_account_get ( struct Address addr,
struct AutocryptAccount **  account 
)

Get Autocrypt Account data from the database.

Parameters
[in]addrEmail Address to lookup
[out]accountMatched account
Return values
0Success
-1Error

Definition at line 265 of file db.c.

266{
267 int rc = -1;
268
269 struct Address *norm_addr = copy_normalize_addr(addr);
270 *account = NULL;
271
272 if (!AccountGetStmt)
273 {
274 if (sqlite3_prepare_v3(AutocryptDB,
275 "SELECT "
276 "email_addr, "
277 "keyid, "
278 "keydata, "
279 "prefer_encrypt, "
280 "enabled "
281 "FROM account "
282 "WHERE email_addr = ?",
283 -1, SQLITE_PREPARE_PERSISTENT, &AccountGetStmt, NULL) != SQLITE_OK)
284 {
285 goto cleanup;
286 }
287 }
288
289 if (sqlite3_bind_text(AccountGetStmt, 1, buf_string(norm_addr->mailbox), -1,
290 SQLITE_STATIC) != SQLITE_OK)
291 {
292 goto cleanup;
293 }
294
295 int result = sqlite3_step(AccountGetStmt);
296 if (result != SQLITE_ROW)
297 {
298 if (result == SQLITE_DONE)
299 rc = 0;
300 goto cleanup;
301 }
302
304 (*account)->email_addr = strdup_column_text(AccountGetStmt, 0);
305 (*account)->keyid = strdup_column_text(AccountGetStmt, 1);
306 (*account)->keydata = strdup_column_text(AccountGetStmt, 2);
307 (*account)->prefer_encrypt = sqlite3_column_int(AccountGetStmt, 3);
308 (*account)->enabled = sqlite3_column_int(AccountGetStmt, 4);
309
310 rc = 1;
311
312cleanup:
313 mutt_addr_free(&norm_addr);
314 sqlite3_reset(AccountGetStmt);
315 return rc;
316}
void mutt_addr_free(struct Address **ptr)
Free a single Address.
Definition: address.c:462
struct AutocryptAccount * mutt_autocrypt_db_account_new(void)
Create a new AutocryptAccount.
Definition: db.c:237
static struct Address * copy_normalize_addr(struct Address *addr)
Copy a normalised Email Address.
Definition: db.c:205
static sqlite3_stmt * AccountGetStmt
Get the matching autocrypt accounts.
Definition: db.c:45
static char * strdup_column_text(sqlite3_stmt *stmt, int index)
Copy a string from the database.
Definition: db.c:227
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_account_get_all()

int mutt_autocrypt_db_account_get_all ( struct AutocryptAccountArray *  aaa)

Get all accounts from an Autocrypt database.

Parameters
aaaAccount array
Return values
nSuccess, number of Accounts
-1Error

Definition at line 461 of file db.c.

462{
463 if (!aaa)
464 return -1;
465
466 int rc = -1;
467 sqlite3_stmt *stmt = NULL;
468
469 /* Note, speed is not of the essence for the account management screen,
470 * so we don't bother with a persistent prepared statement */
471 if (sqlite3_prepare_v2(AutocryptDB,
472 "SELECT "
473 "email_addr, "
474 "keyid, "
475 "keydata, "
476 "prefer_encrypt, "
477 "enabled "
478 "FROM account "
479 "ORDER BY email_addr",
480 -1, &stmt, NULL) != SQLITE_OK)
481 {
482 goto cleanup;
483 }
484
485 int result = SQLITE_ERROR;
486 while ((result = sqlite3_step(stmt)) == SQLITE_ROW)
487 {
489
490 ac->email_addr = strdup_column_text(stmt, 0);
491 ac->keyid = strdup_column_text(stmt, 1);
492 ac->keydata = strdup_column_text(stmt, 2);
493 ac->prefer_encrypt = sqlite3_column_int(stmt, 3);
494 ac->enabled = sqlite3_column_int(stmt, 4);
495
496 ARRAY_ADD(aaa, ac);
497 }
498
499 if (result == SQLITE_DONE)
500 {
501 rc = ARRAY_SIZE(aaa);
502 }
503 else
504 {
505 struct AutocryptAccount **pac = NULL;
506 ARRAY_FOREACH(pac, aaa)
507 {
509 }
510 ARRAY_FREE(aaa);
511 }
512
513cleanup:
514 sqlite3_finalize(stmt);
515 return rc;
516}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:156
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:212
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:87
#define ARRAY_FREE(head)
Release all memory.
Definition: array.h:204
bool enabled
Is this account enabled.
Definition: lib.h:113
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_account_insert()

int mutt_autocrypt_db_account_insert ( struct Address addr,
const char *  keyid,
const char *  keydata,
bool  prefer_encrypt 
)

Insert an Account into the Autocrypt database.

Parameters
addrEmail Address for the account
keyidAutocrypt KeyID
keydataAutocrypt key data
prefer_encryptWhether the account prefers encryption
Return values
0Success
-1Error

Definition at line 327 of file db.c.

329{
330 int rc = -1;
331
332 struct Address *norm_addr = copy_normalize_addr(addr);
333
335 {
336 if (sqlite3_prepare_v3(AutocryptDB,
337 "INSERT INTO account "
338 "(email_addr, "
339 "keyid, "
340 "keydata, "
341 "prefer_encrypt, "
342 "enabled) "
343 "VALUES (?, ?, ?, ?, ?);",
344 -1, SQLITE_PREPARE_PERSISTENT, &AccountInsertStmt, NULL) != SQLITE_OK)
345 {
346 goto cleanup;
347 }
348 }
349
350 if (sqlite3_bind_text(AccountInsertStmt, 1, buf_string(norm_addr->mailbox),
351 -1, SQLITE_STATIC) != SQLITE_OK)
352 {
353 goto cleanup;
354 }
355 if (sqlite3_bind_text(AccountInsertStmt, 2, keyid, -1, SQLITE_STATIC) != SQLITE_OK)
356 goto cleanup;
357 if (sqlite3_bind_text(AccountInsertStmt, 3, keydata, -1, SQLITE_STATIC) != SQLITE_OK)
358 goto cleanup;
359 if (sqlite3_bind_int(AccountInsertStmt, 4, prefer_encrypt) != SQLITE_OK)
360 goto cleanup;
361 if (sqlite3_bind_int(AccountInsertStmt, 5, 1) != SQLITE_OK)
362 goto cleanup;
363
364 if (sqlite3_step(AccountInsertStmt) != SQLITE_DONE)
365 goto cleanup;
366
367 rc = 0;
368
369cleanup:
370 mutt_addr_free(&norm_addr);
371 sqlite3_reset(AccountInsertStmt);
372 return rc;
373}
static sqlite3_stmt * AccountInsertStmt
Insert a new autocrypt account.
Definition: db.c:46
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_account_new()

struct AutocryptAccount * mutt_autocrypt_db_account_new ( void  )

Create a new AutocryptAccount.

Return values
ptrNew AutocryptAccount

Definition at line 237 of file db.c.

238{
239 return MUTT_MEM_CALLOC(1, struct AutocryptAccount);
240}
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_account_update()

int mutt_autocrypt_db_account_update ( struct AutocryptAccount acct)

Update Account info in the Autocrypt database.

Parameters
acctAutocrypt Account data
Return values
0Success
-1Error

Definition at line 381 of file db.c.

382{
383 int rc = -1;
384
386 {
387 if (sqlite3_prepare_v3(AutocryptDB,
388 "UPDATE account SET "
389 "keyid = ?, "
390 "keydata = ?, "
391 "prefer_encrypt = ?, "
392 "enabled = ? "
393 "WHERE email_addr = ?;",
394 -1, SQLITE_PREPARE_PERSISTENT, &AccountUpdateStmt, NULL) != SQLITE_OK)
395 {
396 goto cleanup;
397 }
398 }
399
400 if (sqlite3_bind_text(AccountUpdateStmt, 1, acct->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
401 goto cleanup;
402 if (sqlite3_bind_text(AccountUpdateStmt, 2, acct->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
403 goto cleanup;
404 if (sqlite3_bind_int(AccountUpdateStmt, 3, acct->prefer_encrypt) != SQLITE_OK)
405 goto cleanup;
406 if (sqlite3_bind_int(AccountUpdateStmt, 4, acct->enabled) != SQLITE_OK)
407 goto cleanup;
408 if (sqlite3_bind_text(AccountUpdateStmt, 5, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
409 goto cleanup;
410
411 if (sqlite3_step(AccountUpdateStmt) != SQLITE_DONE)
412 goto cleanup;
413
414 rc = 0;
415
416cleanup:
417 sqlite3_reset(AccountUpdateStmt);
418 return rc;
419}
static sqlite3_stmt * AccountUpdateStmt
Update an autocrypt account.
Definition: db.c:47
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_close()

void mutt_autocrypt_db_close ( void  )

Close the Autocrypt SQLite database connection.

Definition at line 133 of file db.c.

134{
135 if (!AutocryptDB)
136 return;
137
138 sqlite3_finalize(AccountGetStmt);
139 AccountGetStmt = NULL;
140 sqlite3_finalize(AccountInsertStmt);
141 AccountInsertStmt = NULL;
142 sqlite3_finalize(AccountUpdateStmt);
143 AccountUpdateStmt = NULL;
144 sqlite3_finalize(AccountDeleteStmt);
145 AccountDeleteStmt = NULL;
146
147 sqlite3_finalize(PeerGetStmt);
148 PeerGetStmt = NULL;
149 sqlite3_finalize(PeerInsertStmt);
150 PeerInsertStmt = NULL;
151 sqlite3_finalize(PeerUpdateStmt);
152 PeerUpdateStmt = NULL;
153
154 sqlite3_finalize(PeerHistoryInsertStmt);
156
157 sqlite3_finalize(GossipHistoryInsertStmt);
159
160 sqlite3_close_v2(AutocryptDB);
161 AutocryptDB = NULL;
162}
static sqlite3_stmt * PeerUpdateStmt
Update a peer address.
Definition: db.c:51
static sqlite3_stmt * PeerGetStmt
Get the matching peer addresses.
Definition: db.c:49
static sqlite3_stmt * PeerHistoryInsertStmt
Add to the peer history.
Definition: db.c:52
static sqlite3_stmt * GossipHistoryInsertStmt
Add to the gossip history.
Definition: db.c:53
static sqlite3_stmt * PeerInsertStmt
Insert a new peer address.
Definition: db.c:50
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_gossip_history_free()

void mutt_autocrypt_db_gossip_history_free ( struct AutocryptGossipHistory **  ptr)

Free an AutocryptGossipHistory.

Parameters
ptrAutocryptGossipHistory to free

Definition at line 833 of file db.c.

834{
835 if (!ptr || !*ptr)
836 return;
837
838 struct AutocryptGossipHistory *gh = *ptr;
839 FREE(&gh->peer_email_addr);
841 FREE(&gh->email_msgid);
842 FREE(&gh->gossip_keydata);
843 FREE(ptr);
844}
Autocrypt gossip history.
Definition: lib.h:148
char * peer_email_addr
Email addressof the peer.
Definition: lib.h:149
char * email_msgid
Sender's email's message id.
Definition: lib.h:151
char * sender_email_addr
Sender's email address.
Definition: lib.h:150
char * gossip_keydata
Gossip Key data.
Definition: lib.h:153
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_gossip_history_insert()

int mutt_autocrypt_db_gossip_history_insert ( struct Address addr,
struct AutocryptGossipHistory gossip_hist 
)

Insert a gossip history into the Autocrypt database.

Parameters
addrEmail Address
gossip_histGossip history to insert
Return values
0Success
-1Error

Definition at line 853 of file db.c.

855{
856 int rc = -1;
857
858 struct Address *norm_addr = copy_normalize_addr(addr);
859
861 {
862 if (sqlite3_prepare_v3(AutocryptDB,
863 "INSERT INTO gossip_history "
864 "(peer_email_addr, "
865 "sender_email_addr, "
866 "email_msgid, "
867 "timestamp, "
868 "gossip_keydata) "
869 "VALUES (?, ?, ?, ?, ?);",
870 -1, SQLITE_PREPARE_PERSISTENT,
871 &GossipHistoryInsertStmt, NULL) != SQLITE_OK)
872 {
873 goto cleanup;
874 }
875 }
876
877 if (sqlite3_bind_text(GossipHistoryInsertStmt, 1, buf_string(norm_addr->mailbox),
878 -1, SQLITE_STATIC) != SQLITE_OK)
879 {
880 goto cleanup;
881 }
882 if (sqlite3_bind_text(GossipHistoryInsertStmt, 2, gossip_hist->sender_email_addr,
883 -1, SQLITE_STATIC) != SQLITE_OK)
884 {
885 if (sqlite3_bind_text(GossipHistoryInsertStmt, 3, gossip_hist->email_msgid,
886 -1, SQLITE_STATIC) != SQLITE_OK)
887 {
888 goto cleanup;
889 }
890 }
891 if (sqlite3_bind_int64(GossipHistoryInsertStmt, 4, gossip_hist->timestamp) != SQLITE_OK)
892 goto cleanup;
893 if (sqlite3_bind_text(GossipHistoryInsertStmt, 5, gossip_hist->gossip_keydata,
894 -1, SQLITE_STATIC) != SQLITE_OK)
895 {
896 goto cleanup;
897 }
898
899 if (sqlite3_step(GossipHistoryInsertStmt) != SQLITE_DONE)
900 goto cleanup;
901
902 rc = 0;
903
904cleanup:
905 mutt_addr_free(&norm_addr);
906 sqlite3_reset(GossipHistoryInsertStmt);
907 return rc;
908}
sqlite3_int64 timestamp
Timestamp of sender's email.
Definition: lib.h:152
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_gossip_history_new()

struct AutocryptGossipHistory * mutt_autocrypt_db_gossip_history_new ( void  )

Create a new AutocryptGossipHistory.

Return values
ptrNew AutocryptGossipHistory

Definition at line 824 of file db.c.

825{
826 return MUTT_MEM_CALLOC(1, struct AutocryptGossipHistory);
827}
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_init()

int mutt_autocrypt_db_init ( bool  can_create)

Initialise the Autocrypt SQLite database.

Parameters
can_createIf true, the directory may be created
Return values
0Success
-1Error

Definition at line 83 of file db.c.

84{
85 int rc = -1;
86
87 if (AutocryptDB)
88 return 0;
89
90 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
91 const char *const c_autocrypt_dir = cs_subset_path(NeoMutt->sub, "autocrypt_dir");
92 if (!c_autocrypt || !c_autocrypt_dir)
93 return -1;
94
95 struct Buffer *db_path = buf_pool_get();
96 buf_concat_path(db_path, c_autocrypt_dir, "autocrypt.db");
97
98 struct stat st = { 0 };
99 if (stat(buf_string(db_path), &st) == 0)
100 {
101 if (sqlite3_open_v2(buf_string(db_path), &AutocryptDB, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK)
102 {
103 /* L10N: autocrypt couldn't open the SQLite database.
104 The %s is the full path of the database file. */
105 mutt_error(_("Unable to open autocrypt database %s"), buf_string(db_path));
106 goto cleanup;
107 }
108
110 goto cleanup;
111 }
112 else
113 {
114 if (!can_create)
115 goto cleanup;
116 if (autocrypt_db_create(buf_string(db_path)))
117 goto cleanup;
118 /* Don't abort the whole init process because account creation failed */
121 }
122
123 rc = 0;
124
125cleanup:
126 buf_pool_release(&db_path);
127 return rc;
128}
static int autocrypt_db_create(const char *db_path)
Create an Autocrypt SQLite database.
Definition: db.c:64
int mutt_autocrypt_schema_update(void)
Update the version number of the Autocrypt database schema.
Definition: schema.c:107
int mutt_autocrypt_account_init(bool prompt)
Create a new Autocrypt account.
Definition: autocrypt.c:143
void mutt_autocrypt_scan_mailboxes(void)
Scan mailboxes for Autocrypt headers.
Definition: autocrypt.c:914
size_t buf_concat_path(struct Buffer *buf, const char *dir, const char *fname)
Join a directory name and a filename.
Definition: buffer.c:509
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_normalize_addr()

void mutt_autocrypt_db_normalize_addr ( struct Address a)

Normalise an Email Address.

Parameters
aAddress to normalise

Definition at line 168 of file db.c.

169{
171 buf_lower(a->mailbox);
173}
bool mutt_addr_to_local(struct Address *a)
Convert an Address from Punycode.
Definition: address.c:1340
bool mutt_addr_to_intl(struct Address *a)
Convert an Address to Punycode.
Definition: address.c:1263
void buf_lower(struct Buffer *buf)
Sets a buffer to lowercase.
Definition: buffer.c:736
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_normalize_addrlist()

void mutt_autocrypt_db_normalize_addrlist ( struct AddressList *  al)

Normalise a list of Email Addresses.

Parameters
alList of Addresses to normalise

Definition at line 179 of file db.c.

180{
182
183 struct Address *np = NULL;
184 TAILQ_FOREACH(np, al, entries)
185 {
186 buf_lower(np->mailbox);
187 }
188
189 mutt_addrlist_to_intl(al, NULL);
190}
int mutt_addrlist_to_local(struct AddressList *al)
Convert an Address list from Punycode.
Definition: address.c:1378
int mutt_addrlist_to_intl(struct AddressList *al, char **err)
Convert an Address list to Punycode.
Definition: address.c:1293
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_free()

void mutt_autocrypt_db_peer_free ( struct AutocryptPeer **  ptr)

Free an AutocryptPeer.

Parameters
ptrAutocryptPeer to free

Definition at line 531 of file db.c.

532{
533 if (!ptr || !*ptr)
534 return;
535
536 struct AutocryptPeer *peer = *ptr;
537 FREE(&peer->email_addr);
538 FREE(&peer->keyid);
539 FREE(&peer->keydata);
540 FREE(&peer->gossip_keyid);
541 FREE(&peer->gossip_keydata);
542 FREE(ptr);
543}
Autocrypt peer.
Definition: lib.h:121
char * gossip_keydata
Gossip Key data.
Definition: lib.h:130
char * keyid
PGP Key id.
Definition: lib.h:125
char * gossip_keyid
Gossip Key id.
Definition: lib.h:129
char * keydata
PGP Key data.
Definition: lib.h:126
char * email_addr
Email address.
Definition: lib.h:122
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_get()

int mutt_autocrypt_db_peer_get ( struct Address addr,
struct AutocryptPeer **  peer 
)

Get peer info from the Autocrypt database.

Parameters
[in]addrEmail Address to look up
[out]peerMatching Autocrypt Peer
Return values
0Success, no matches
1Success, a match
-1Error

Definition at line 553 of file db.c.

554{
555 int rc = -1;
556
557 struct Address *norm_addr = copy_normalize_addr(addr);
558 *peer = NULL;
559
560 if (!PeerGetStmt)
561 {
562 if (sqlite3_prepare_v3(AutocryptDB,
563 "SELECT "
564 "email_addr, "
565 "last_seen, "
566 "autocrypt_timestamp, "
567 "keyid, "
568 "keydata, "
569 "prefer_encrypt, "
570 "gossip_timestamp, "
571 "gossip_keyid, "
572 "gossip_keydata "
573 "FROM peer "
574 "WHERE email_addr = ?",
575 -1, SQLITE_PREPARE_PERSISTENT, &PeerGetStmt, NULL) != SQLITE_OK)
576 {
577 goto cleanup;
578 }
579 }
580
581 if (sqlite3_bind_text(PeerGetStmt, 1, buf_string(norm_addr->mailbox), -1,
582 SQLITE_STATIC) != SQLITE_OK)
583 {
584 goto cleanup;
585 }
586
587 int result = sqlite3_step(PeerGetStmt);
588 if (result != SQLITE_ROW)
589 {
590 if (result == SQLITE_DONE)
591 rc = 0;
592 goto cleanup;
593 }
594
596 (*peer)->email_addr = strdup_column_text(PeerGetStmt, 0);
597 (*peer)->last_seen = sqlite3_column_int64(PeerGetStmt, 1);
598 (*peer)->autocrypt_timestamp = sqlite3_column_int64(PeerGetStmt, 2);
599 (*peer)->keyid = strdup_column_text(PeerGetStmt, 3);
600 (*peer)->keydata = strdup_column_text(PeerGetStmt, 4);
601 (*peer)->prefer_encrypt = sqlite3_column_int(PeerGetStmt, 5);
602 (*peer)->gossip_timestamp = sqlite3_column_int64(PeerGetStmt, 6);
603 (*peer)->gossip_keyid = strdup_column_text(PeerGetStmt, 7);
604 (*peer)->gossip_keydata = strdup_column_text(PeerGetStmt, 8);
605
606 rc = 1;
607
608cleanup:
609 mutt_addr_free(&norm_addr);
610 sqlite3_reset(PeerGetStmt);
611 return rc;
612}
struct AutocryptPeer * mutt_autocrypt_db_peer_new(void)
Create a new AutocryptPeer.
Definition: db.c:522
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_history_free()

void mutt_autocrypt_db_peer_history_free ( struct AutocryptPeerHistory **  ptr)

Free an AutocryptPeerHistory.

Parameters
ptrAutocryptPeerHistory to free

Definition at line 752 of file db.c.

753{
754 if (!ptr || !*ptr)
755 return;
756
757 struct AutocryptPeerHistory *ph = *ptr;
758 FREE(&ph->peer_email_addr);
759 FREE(&ph->email_msgid);
760 FREE(&ph->keydata);
761 FREE(ptr);
762}
Autocrypt peer history.
Definition: lib.h:137
char * peer_email_addr
Email address of the peer.
Definition: lib.h:138
char * email_msgid
Message id of the email.
Definition: lib.h:139
char * keydata
PGP Key data.
Definition: lib.h:141
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_history_insert()

int mutt_autocrypt_db_peer_history_insert ( struct Address addr,
struct AutocryptPeerHistory peerhist 
)

Insert peer history into the Autocrypt database.

Parameters
addrEmail Address
peerhistPeer history to insert
Return values
0Success
-1Error

Definition at line 771 of file db.c.

773{
774 int rc = -1;
775
776 struct Address *norm_addr = copy_normalize_addr(addr);
777
779 {
780 if (sqlite3_prepare_v3(AutocryptDB,
781 "INSERT INTO peer_history "
782 "(peer_email_addr, "
783 "email_msgid, "
784 "timestamp, "
785 "keydata) "
786 "VALUES (?, ?, ?, ?);",
787 -1, SQLITE_PREPARE_PERSISTENT,
788 &PeerHistoryInsertStmt, NULL) != SQLITE_OK)
789 {
790 goto cleanup;
791 }
792 }
793
794 if (sqlite3_bind_text(PeerHistoryInsertStmt, 1, buf_string(norm_addr->mailbox),
795 -1, SQLITE_STATIC) != SQLITE_OK)
796 {
797 goto cleanup;
798 }
799 if (sqlite3_bind_text(PeerHistoryInsertStmt, 2, peerhist->email_msgid, -1,
800 SQLITE_STATIC) != SQLITE_OK)
801 {
802 goto cleanup;
803 }
804 if (sqlite3_bind_int64(PeerHistoryInsertStmt, 3, peerhist->timestamp) != SQLITE_OK)
805 goto cleanup;
806 if (sqlite3_bind_text(PeerHistoryInsertStmt, 4, peerhist->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
807 goto cleanup;
808
809 if (sqlite3_step(PeerHistoryInsertStmt) != SQLITE_DONE)
810 goto cleanup;
811
812 rc = 0;
813
814cleanup:
815 mutt_addr_free(&norm_addr);
816 sqlite3_reset(PeerHistoryInsertStmt);
817 return rc;
818}
sqlite3_int64 timestamp
Timestamp of email.
Definition: lib.h:140
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_history_new()

struct AutocryptPeerHistory * mutt_autocrypt_db_peer_history_new ( void  )

Create a new AutocryptPeerHistory.

Return values
ptrNew AutocryptPeerHistory

Definition at line 743 of file db.c.

744{
745 return MUTT_MEM_CALLOC(1, struct AutocryptPeerHistory);
746}
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_insert()

int mutt_autocrypt_db_peer_insert ( struct Address addr,
struct AutocryptPeer peer 
)

Insert a peer into the Autocrypt database.

Parameters
addrEmail Address
peerAutocryptPeer to insert
Return values
0Success
-1Error

Definition at line 621 of file db.c.

622{
623 int rc = -1;
624 struct Address *norm_addr = NULL;
625
626 norm_addr = copy_normalize_addr(addr);
627
628 if (!PeerInsertStmt)
629 {
630 if (sqlite3_prepare_v3(AutocryptDB,
631 "INSERT INTO peer "
632 "(email_addr, "
633 "last_seen, "
634 "autocrypt_timestamp, "
635 "keyid, "
636 "keydata, "
637 "prefer_encrypt, "
638 "gossip_timestamp, "
639 "gossip_keyid, "
640 "gossip_keydata) "
641 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);",
642 -1, SQLITE_PREPARE_PERSISTENT, &PeerInsertStmt, NULL) != SQLITE_OK)
643 {
644 goto cleanup;
645 }
646 }
647
648 if (sqlite3_bind_text(PeerInsertStmt, 1, buf_string(norm_addr->mailbox), -1,
649 SQLITE_STATIC) != SQLITE_OK)
650 {
651 goto cleanup;
652 }
653 if (sqlite3_bind_int64(PeerInsertStmt, 2, peer->last_seen) != SQLITE_OK)
654 goto cleanup;
655 if (sqlite3_bind_int64(PeerInsertStmt, 3, peer->autocrypt_timestamp) != SQLITE_OK)
656 goto cleanup;
657 if (sqlite3_bind_text(PeerInsertStmt, 4, peer->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
658 goto cleanup;
659 if (sqlite3_bind_text(PeerInsertStmt, 5, peer->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
660 goto cleanup;
661 if (sqlite3_bind_int(PeerInsertStmt, 6, peer->prefer_encrypt) != SQLITE_OK)
662 goto cleanup;
663 if (sqlite3_bind_int64(PeerInsertStmt, 7, peer->gossip_timestamp) != SQLITE_OK)
664 goto cleanup;
665 if (sqlite3_bind_text(PeerInsertStmt, 8, peer->gossip_keyid, -1, SQLITE_STATIC) != SQLITE_OK)
666 goto cleanup;
667 if (sqlite3_bind_text(PeerInsertStmt, 9, peer->gossip_keydata, -1, SQLITE_STATIC) != SQLITE_OK)
668 goto cleanup;
669
670 if (sqlite3_step(PeerInsertStmt) != SQLITE_DONE)
671 goto cleanup;
672
673 rc = 0;
674
675cleanup:
676 mutt_addr_free(&norm_addr);
677 sqlite3_reset(PeerInsertStmt);
678 return rc;
679}
sqlite3_int64 autocrypt_timestamp
When the email was sent.
Definition: lib.h:124
sqlite3_int64 last_seen
When was the peer last seen.
Definition: lib.h:123
bool prefer_encrypt
false = nopref, true = mutual
Definition: lib.h:127
sqlite3_int64 gossip_timestamp
Timestamp of Gossip header.
Definition: lib.h:128
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_new()

struct AutocryptPeer * mutt_autocrypt_db_peer_new ( void  )

Create a new AutocryptPeer.

Return values
ptrNew AutocryptPeer

Definition at line 522 of file db.c.

523{
524 return MUTT_MEM_CALLOC(1, struct AutocryptPeer);
525}
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_peer_update()

int mutt_autocrypt_db_peer_update ( struct AutocryptPeer peer)

Update the peer info in an Autocrypt database.

Parameters
peerAutocryptPeer to update
Return values
0Success
-1Error

Definition at line 687 of file db.c.

688{
689 int rc = -1;
690
691 if (!PeerUpdateStmt)
692 {
693 if (sqlite3_prepare_v3(AutocryptDB,
694 "UPDATE peer SET "
695 "last_seen = ?, "
696 "autocrypt_timestamp = ?, "
697 "keyid = ?, "
698 "keydata = ?, "
699 "prefer_encrypt = ?, "
700 "gossip_timestamp = ?, "
701 "gossip_keyid = ?, "
702 "gossip_keydata = ? "
703 "WHERE email_addr = ?;",
704 -1, SQLITE_PREPARE_PERSISTENT, &PeerUpdateStmt, NULL) != SQLITE_OK)
705 {
706 goto cleanup;
707 }
708 }
709
710 if (sqlite3_bind_int64(PeerUpdateStmt, 1, peer->last_seen) != SQLITE_OK)
711 goto cleanup;
712 if (sqlite3_bind_int64(PeerUpdateStmt, 2, peer->autocrypt_timestamp) != SQLITE_OK)
713 goto cleanup;
714 if (sqlite3_bind_text(PeerUpdateStmt, 3, peer->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
715 goto cleanup;
716 if (sqlite3_bind_text(PeerUpdateStmt, 4, peer->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
717 goto cleanup;
718 if (sqlite3_bind_int(PeerUpdateStmt, 5, peer->prefer_encrypt) != SQLITE_OK)
719 goto cleanup;
720 if (sqlite3_bind_int64(PeerUpdateStmt, 6, peer->gossip_timestamp) != SQLITE_OK)
721 goto cleanup;
722 if (sqlite3_bind_text(PeerUpdateStmt, 7, peer->gossip_keyid, -1, SQLITE_STATIC) != SQLITE_OK)
723 goto cleanup;
724 if (sqlite3_bind_text(PeerUpdateStmt, 8, peer->gossip_keydata, -1, SQLITE_STATIC) != SQLITE_OK)
725 goto cleanup;
726 if (sqlite3_bind_text(PeerUpdateStmt, 9, peer->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
727 goto cleanup;
728
729 if (sqlite3_step(PeerUpdateStmt) != SQLITE_DONE)
730 goto cleanup;
731
732 rc = 0;
733
734cleanup:
735 sqlite3_reset(PeerUpdateStmt);
736 return rc;
737}
+ Here is the caller graph for this function:

◆ mutt_autocrypt_schema_init()

int mutt_autocrypt_schema_init ( void  )

Set up an Autocrypt database.

Return values
0Success
-1Error

Definition at line 41 of file schema.c.

42{
43 char *errmsg = NULL;
44
45 const char *schema = "BEGIN TRANSACTION; "
46
47 "CREATE TABLE account ("
48 "email_addr text primary key not null, "
49 "keyid text, "
50 "keydata text, "
51 "prefer_encrypt int, "
52 "enabled int);"
53
54 "CREATE TABLE peer ("
55 "email_addr text primary key not null, "
56 "last_seen int, "
57 "autocrypt_timestamp int, "
58 "keyid text, "
59 "keydata text, "
60 "prefer_encrypt int, "
61 "gossip_timestamp int, "
62 "gossip_keyid text, "
63 "gossip_keydata text);"
64
65 "CREATE TABLE peer_history ("
66 "peer_email_addr text not null, "
67 "email_msgid text, "
68 "timestamp int, "
69 "keydata text);"
70
71 "CREATE INDEX peer_history_email "
72 "ON peer_history ("
73 "peer_email_addr);"
74
75 "CREATE TABLE gossip_history ("
76 "peer_email_addr text not null, "
77 "sender_email_addr text, "
78 "email_msgid text, "
79 "timestamp int, "
80 "gossip_keydata text);"
81
82 "CREATE INDEX gossip_history_email "
83 "ON gossip_history ("
84 "peer_email_addr);"
85
86 "CREATE TABLE schema ("
87 "version int);"
88
89 "INSERT into schema (version) values (1);"
90
91 "COMMIT TRANSACTION";
92
93 if (sqlite3_exec(AutocryptDB, schema, NULL, NULL, &errmsg) != SQLITE_OK)
94 {
95 mutt_debug(LL_DEBUG1, "mutt_autocrypt_schema_init() returned %s\n", errmsg);
96 sqlite3_free(errmsg);
97 return -1;
98 }
99 return 0;
100}
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
+ Here is the caller graph for this function:

◆ mutt_autocrypt_schema_update()

int mutt_autocrypt_schema_update ( void  )

Update the version number of the Autocrypt database schema.

Return values
0Success
-1Error

Definition at line 107 of file schema.c.

108{
109 sqlite3_stmt *stmt = NULL;
110 int rc = -1;
111
112 if (sqlite3_prepare_v2(AutocryptDB, "SELECT version FROM schema;", -1, &stmt, NULL) != SQLITE_OK)
113 goto cleanup;
114
115 if (sqlite3_step(stmt) != SQLITE_ROW)
116 goto cleanup;
117
118 int version = sqlite3_column_int(stmt, 0);
119
120 if (version > 1)
121 {
122 /* L10N: The autocrypt database keeps track of schema version numbers.
123 This error occurs if the version number is too high.
124 Presumably because this is an old version of NeoMutt and the
125 database was upgraded by a future version. */
126 mutt_error(_("Autocrypt database version is too new"));
127 goto cleanup;
128 }
129
130 /* TODO: schema version upgrades go here.
131 * Bump one by one, each update inside a transaction. */
132
133 rc = 0;
134
135cleanup:
136 sqlite3_finalize(stmt);
137 return rc;
138}
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_create_key()

int mutt_autocrypt_gpgme_create_key ( struct Address addr,
struct Buffer keyid,
struct Buffer keydata 
)

Create a GPGME key.

Parameters
addrEmail Address
keyidKey id
keydataKey data
Return values
0Success
-1Error

Definition at line 158 of file gpgme.c.

160{
161 int rc = -1;
162 gpgme_ctx_t ctx = NULL;
163 gpgme_genkey_result_t keyresult = NULL;
164 gpgme_key_t primary_key = NULL;
165 struct Buffer *buf = buf_pool_get();
166
167 /* GPGME says addresses should not be in idna form */
168 struct Address *copy = mutt_addr_copy(addr);
169 mutt_addr_to_local(copy);
170 mutt_addr_write(buf, copy, false);
171 mutt_addr_free(&copy);
172
173 if (create_gpgme_context(&ctx))
174 goto cleanup;
175
176 /* L10N: Message displayed just before a GPG key is generated for a created
177 autocrypt account. */
178 mutt_message(_("Generating autocrypt key..."));
179
180 /* Primary key */
181 gpgme_error_t err = gpgme_op_createkey(ctx, buf_string(buf), "ed25519", 0, 0, NULL,
182 GPGME_CREATE_NOPASSWD | GPGME_CREATE_FORCE |
183 GPGME_CREATE_NOEXPIRE);
184 if (err != GPG_ERR_NO_ERROR)
185 {
186 /* L10N: GPGME was unable to generate a key for some reason.
187 %s is the error message returned by GPGME. */
188 mutt_error(_("Error creating autocrypt key: %s"), gpgme_strerror(err));
189 goto cleanup;
190 }
191 keyresult = gpgme_op_genkey_result(ctx);
192 if (!keyresult->fpr)
193 goto cleanup;
194 buf_strcpy(keyid, keyresult->fpr);
195 mutt_debug(LL_DEBUG1, "Generated key with id %s\n", buf_string(keyid));
196
197 /* Get gpgme_key_t to create the secondary key and export keydata */
198 err = gpgme_get_key(ctx, buf_string(keyid), &primary_key, 0);
199 if (err)
200 goto cleanup;
201
202 /* Secondary key */
203 err = gpgme_op_createsubkey(ctx, primary_key, "cv25519", 0, 0,
204 GPGME_CREATE_NOPASSWD | GPGME_CREATE_NOEXPIRE);
205 if (err != GPG_ERR_NO_ERROR)
206 {
207 mutt_error(_("Error creating autocrypt key: %s"), gpgme_strerror(err));
208 goto cleanup;
209 }
210
211 /* get keydata */
212 if (export_keydata(ctx, primary_key, keydata))
213 goto cleanup;
214 mutt_debug(LL_DEBUG1, "key has keydata *%s*\n", buf_string(keydata));
215
216 rc = 0;
217
218cleanup:
219 gpgme_key_unref(primary_key);
220 gpgme_release(ctx);
221 buf_pool_release(&buf);
222 return rc;
223}
size_t mutt_addr_write(struct Buffer *buf, struct Address *addr, bool display)
Write a single Address to a buffer.
Definition: address.c:1050
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:395
static int create_gpgme_context(gpgme_ctx_t *ctx)
Create a GPGME context.
Definition: gpgme.c:51
static int export_keydata(gpgme_ctx_t ctx, gpgme_key_t key, struct Buffer *keydata)
Export Key data from GPGME into a Buffer.
Definition: gpgme.c:84
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_import_key()

int mutt_autocrypt_gpgme_import_key ( const char *  keydata,
struct Buffer keyid 
)

Read a key from GPGME.

Parameters
keydataBuffer for key data
keyidBuffer for key id
Return values
0Success
-1Error

Definition at line 320 of file gpgme.c.

321{
322 int rc = -1;
323 gpgme_ctx_t ctx = NULL;
324 gpgme_data_t dh = NULL;
325 struct Buffer *raw_keydata = NULL;
326
327 if (create_gpgme_context(&ctx))
328 goto cleanup;
329
330 raw_keydata = buf_pool_get();
331 if (!mutt_b64_buffer_decode(raw_keydata, keydata))
332 goto cleanup;
333
334 if (gpgme_data_new_from_mem(&dh, buf_string(raw_keydata), buf_len(raw_keydata), 0))
335 {
336 goto cleanup;
337 }
338
339 if (gpgme_op_import(ctx, dh))
340 goto cleanup;
341
342 gpgme_import_result_t result = gpgme_op_import_result(ctx);
343 if (!result->imports || !result->imports->fpr)
344 goto cleanup;
345 buf_strcpy(keyid, result->imports->fpr);
346
347 rc = 0;
348
349cleanup:
350 gpgme_data_release(dh);
351 gpgme_release(ctx);
352 buf_pool_release(&raw_keydata);
353 return rc;
354}
int mutt_b64_buffer_decode(struct Buffer *buf, const char *in)
Convert null-terminated base64 string to raw bytes.
Definition: base64.c:216
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:491
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_init()

int mutt_autocrypt_gpgme_init ( void  )

Initialise GPGME.

Return values
0Always

Definition at line 70 of file gpgme.c.

71{
73 return 0;
74}
void pgp_gpgme_init(void)
Initialise the crypto module - Implements CryptModuleSpecs::init() -.
Definition: crypt_gpgme.c:3865
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_is_valid_key()

bool mutt_autocrypt_gpgme_is_valid_key ( const char *  keyid)

Is a key id valid?

Parameters
keyidKey id to check
Return values
trueKey id is valid

Definition at line 361 of file gpgme.c.

362{
363 bool rc = false;
364 gpgme_ctx_t ctx = NULL;
365 gpgme_key_t key = NULL;
366
367 if (!keyid)
368 return false;
369
370 if (create_gpgme_context(&ctx))
371 goto cleanup;
372
373 if (gpgme_get_key(ctx, keyid, &key, 0))
374 goto cleanup;
375
376 rc = true;
377 if (key->revoked || key->expired || key->disabled || key->invalid || !key->can_encrypt)
378 rc = false;
379
380cleanup:
381 gpgme_key_unref(key);
382 gpgme_release(ctx);
383 return rc;
384}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_select_key()

int mutt_autocrypt_gpgme_select_key ( struct Buffer keyid,
struct Buffer keydata 
)

Select a Autocrypt key.

Parameters
[in]keyidKey id to select
[out]keydataBuffer for resulting Key data
Return values
0Success
-1Error

Definition at line 232 of file gpgme.c.

233{
234 int rc = -1;
235 gpgme_ctx_t ctx = NULL;
236 gpgme_key_t key = NULL;
237
238 OptAutocryptGpgme = true;
240 goto cleanup;
241
242 if (create_gpgme_context(&ctx))
243 goto cleanup;
244
245 if (gpgme_get_key(ctx, buf_string(keyid), &key, 0))
246 goto cleanup;
247
248 if (key->revoked || key->expired || key->disabled || key->invalid ||
249 !key->can_encrypt || !key->can_sign)
250 {
251 /* L10N: After selecting a key for an autocrypt account,
252 this is displayed if the key was revoked/expired/disabled/invalid
253 or can't be used for both signing and encryption.
254 %s is the key fingerprint. */
255 mutt_error(_("The key %s is not usable for autocrypt"), buf_string(keyid));
256 goto cleanup;
257 }
258
259 if (export_keydata(ctx, key, keydata))
260 goto cleanup;
261
262 rc = 0;
263
264cleanup:
265 OptAutocryptGpgme = false;
266 gpgme_key_unref(key);
267 gpgme_release(ctx);
268 return rc;
269}
int mutt_gpgme_select_secret_key(struct Buffer *keyid)
Select a private Autocrypt key for a new account.
Definition: crypt_gpgme.c:3685
bool OptAutocryptGpgme
(pseudo) use Autocrypt context inside ncrypt/crypt_gpgme.c
Definition: globals.c:59
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_gpgme_select_or_create_key()

int mutt_autocrypt_gpgme_select_or_create_key ( struct Address addr,
struct Buffer keyid,
struct Buffer keydata 
)

Ask the user to select or create an Autocrypt key.

Parameters
addrEmail Address
keyidKey id
keydataKey data
Return values
0Success
-1Error

Definition at line 279 of file gpgme.c.

281{
282 int rc = -1;
283
284 /* L10N: During autocrypt account creation, this prompt asks the
285 user whether they want to create a new GPG key for the account,
286 or select an existing account from the keyring. */
287 const char *prompt = _("(c)reate new, or (s)elect existing GPG key?");
288 /* L10N: The letters corresponding to the
289 "(c)reate new, or (s)elect existing GPG key?" prompt. */
290 const char *letters = _("cs");
291
292 int choice = mw_multi_choice(prompt, letters);
293 switch (choice)
294 {
295 case 2: /* select existing */
296 rc = mutt_autocrypt_gpgme_select_key(keyid, keydata);
297 if (rc == 0)
298 break;
299
300 /* L10N: During autocrypt account creation, if selecting an existing key fails
301 for some reason, we prompt to see if they want to create a key instead. */
302 if (query_yesorno(_("Create a new GPG key for this account, instead?"), MUTT_YES) != MUTT_YES)
303 break;
305
306 case 1: /* create new */
307 rc = mutt_autocrypt_gpgme_create_key(addr, keyid, keydata);
308 }
309
310 return rc;
311}
int mutt_autocrypt_gpgme_create_key(struct Address *addr, struct Buffer *keyid, struct Buffer *keydata)
Create a GPGME key.
Definition: gpgme.c:158
int mutt_autocrypt_gpgme_select_key(struct Buffer *keyid, struct Buffer *keydata)
Select a Autocrypt key.
Definition: gpgme.c:232
int mw_multi_choice(const char *prompt, const char *letters)
Offer the user a multiple choice question -.
Definition: question.c:63
#define FALLTHROUGH
Definition: lib.h:111
enum QuadOption query_yesorno(const char *prompt, enum QuadOption def)
Ask the user a Yes/No question.
Definition: question.c:327
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ populate_menu()

bool populate_menu ( struct Menu menu)

Add the Autocrypt data to a Menu.

Parameters
menuMenu to populate
Return values
trueSuccess

Definition at line 223 of file dlg_autocrypt.c.

224{
225 struct AutocryptData *ad = menu->mdata;
226
227 // Clear out any existing data
229 menu->max = 0;
230
231 struct AutocryptAccountArray accounts = ARRAY_HEAD_INITIALIZER;
232
233 if (mutt_autocrypt_db_account_get_all(&accounts) < 0)
234 return false;
235
236 menu->max = ARRAY_SIZE(&accounts);
237
238 struct AutocryptAccount **pac = NULL;
239 ARRAY_FOREACH(pac, &accounts)
240 {
241 struct AccountEntry *entry = MUTT_MEM_CALLOC(1, struct AccountEntry);
242
243 entry->num = ARRAY_FOREACH_IDX + 1;
244 /* note: we are transferring the account pointer to the entries
245 * array, and freeing the accounts array below. the account
246 * will be freed in autocrypt_menu_free(). */
247 entry->account = *pac;
248
249 entry->addr = mutt_addr_new();
250 entry->addr->mailbox = buf_new((*pac)->email_addr);
251 mutt_addr_to_local(entry->addr);
252 ARRAY_ADD(&ad->entries, entry);
253 }
254 ARRAY_FREE(&accounts);
255
257 return true;
258}
struct Address * mutt_addr_new(void)
Create a new Address.
Definition: address.c:401
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition: array.h:58
int mutt_autocrypt_db_account_get_all(struct AutocryptAccountArray *aaa)
Get all accounts from an Autocrypt database.
Definition: db.c:461
void account_entry_array_clear(struct AccountEntryArray *entries)
Clear an AccountEntry array.
#define MENU_REDRAW_FULL
Redraw everything.
Definition: lib.h:59
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition: menu.c:184
An entry in the Autocrypt account Menu.
Definition: private.h:47
struct Address * addr
Email address associated with the account.
Definition: private.h:50
struct AutocryptAccount * account
Account details.
Definition: private.h:49
int num
Number in the index.
Definition: private.h:48
Data to pass to the Autocrypt Functions.
struct Menu * menu
Autocrypt Menu.
struct AccountEntryArray entries
Account Entries.
void * mdata
Private data.
Definition: lib.h:147
int max
Number of entries in the menu.
Definition: lib.h:81
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ AutocryptDB

sqlite3* AutocryptDB
extern

Handle to the open Autocrypt database.

Definition at line 56 of file db.c.