NeoMutt  2025-12-11-860-g80c9cc
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 (struct AutocryptModuleData *mod_data)
 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.
 

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 57 of file private.h.

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

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 150 of file autocrypt.c.

151{
152 struct Address *addr = NULL;
153 struct AutocryptAccount *account = NULL;
154 bool done = false;
155 int rc = -1;
156 bool prefer_encrypt = false;
157
158 if (prompt)
159 {
160 /* L10N: The first time NeoMutt is started with $autocrypt set, it will
161 create $autocrypt_dir and then prompt to create an autocrypt account
162 with this message. */
163 if (query_yesorno_ignore_macro(_("Create an initial autocrypt account?"), MUTT_YES) != MUTT_YES)
164 return 0;
165 }
166
167 struct Buffer *keyid = buf_pool_get();
168 struct Buffer *keydata = buf_pool_get();
169
170 const struct Address *c_from = cs_subset_address(NeoMutt->sub, "from");
171 if (c_from)
172 {
173 addr = mutt_addr_copy(c_from);
174 const char *const c_real_name = cs_subset_string(NeoMutt->sub, "real_name");
175 if (!addr->personal && c_real_name)
176 addr->personal = buf_new(c_real_name);
177 }
178
179 struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
180 mutt_addrlist_append(&al, addr);
181
182 do
183 {
184 /* L10N: Autocrypt is asking for the email address to use for the
185 autocrypt account. This will generate a key and add a record
186 to the database for use in autocrypt operations. */
187 if (mutt_edit_address(&al, _("Autocrypt account address: "), false) != 0)
188 goto cleanup;
189
190 addr = TAILQ_FIRST(&al);
191 if (!addr || !addr->mailbox || TAILQ_NEXT(addr, entries))
192 {
193 /* L10N: Autocrypt prompts for an account email address, and requires
194 a single address. This is shown if they entered something invalid,
195 nothing, or more than one address for some reason. */
196 mutt_error(_("Please enter a single email address"));
197 done = false;
198 }
199 else
200 {
201 done = true;
202 }
203 } while (!done);
204
205 addr = TAILQ_FIRST(&al);
206 if (mutt_autocrypt_db_account_get(addr, &account) < 0)
207 goto cleanup;
208 if (account)
209 {
210 /* L10N: When creating an autocrypt account, this message will be displayed
211 if there is already an account in the database with the email address
212 they just entered. */
213 mutt_error(_("That email address already has an autocrypt account"));
214 goto cleanup;
215 }
216
217 if (mutt_autocrypt_gpgme_select_or_create_key(addr, keyid, keydata))
218 goto cleanup;
219
220 /* L10N: Autocrypt has a setting "prefer-encrypt".
221 When the recommendation algorithm returns "available" and BOTH sender and
222 recipient choose "prefer-encrypt", encryption will be automatically
223 enabled.
224 Otherwise the UI will show encryption is "available" but the user
225 will be required to enable encryption manually. */
226 if (query_yesorno_ignore_macro(_("Prefer encryption?"), MUTT_NO) == MUTT_YES)
227 prefer_encrypt = true;
228
229 if (mutt_autocrypt_db_account_insert(addr, buf_string(keyid), buf_string(keydata), prefer_encrypt))
230 {
231 goto cleanup;
232 }
233
234 rc = 0;
235
236cleanup:
237 if (rc == 0)
238 {
239 /* L10N: Message displayed after an autocrypt account is successfully created. */
240 mutt_message(_("Autocrypt account creation succeeded"));
241 }
242 else
243 {
244 /* L10N: Error message displayed if creating an autocrypt account failed
245 or was aborted by the user. */
246 mutt_error(_("Autocrypt account creation aborted"));
247 }
248
251 buf_pool_release(&keyid);
252 buf_pool_release(&keydata);
253 return rc;
254}
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition address.c:1469
void mutt_addrlist_append(struct AddressList *al, struct Address *a)
Append an Address to an AddressList.
Definition address.c:1489
struct Address * mutt_addr_copy(const struct Address *addr)
Copy the real address.
Definition address.c:754
const struct Address * cs_subset_address(const struct ConfigSubset *sub, const char *name)
Get an Address config item by name.
int mutt_autocrypt_db_account_get(struct Address *addr, struct AutocryptAccount **account)
Get Autocrypt Account data from the database.
Definition db.c:259
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:325
void mutt_autocrypt_db_account_free(struct AutocryptAccount **ptr)
Free an AutocryptAccount.
Definition db.c:240
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:94
#define mutt_message(...)
Definition logging2.h:93
#define _(a)
Definition message.h:28
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
@ 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:342
#define TAILQ_FIRST(head)
Definition queue.h:780
#define TAILQ_NEXT(elm, field)
Definition queue.h:889
#define TAILQ_HEAD_INITIALIZER(head)
Definition queue.h:694
int mutt_edit_address(struct AddressList *al, const char *field, bool expand_aliases)
Edit an email address.
Definition send.c:184
An email address.
Definition address.h:35
struct Buffer * personal
Real name of address.
Definition address.h:36
struct Buffer * mailbox
Mailbox and host address.
Definition address.h:37
Autocrypt account.
Definition lib.h:114
bool prefer_encrypt
false = nopref, true = mutual
Definition lib.h:118
String manipulation buffer.
Definition buffer.h:36
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
+ 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 937 of file autocrypt.c.

938{
939#ifdef USE_HCACHE
940 const char *c_header_cache = cs_subset_path(NeoMutt->sub, "header_cache");
941 char *old_hdrcache = mutt_str_dup(c_header_cache);
942 cs_subset_str_native_set(NeoMutt->sub, "header_cache", 0, NULL);
943#endif
944
945 struct Buffer *folderbuf = buf_pool_get();
946
947 /* L10N: The first time autocrypt is enabled, NeoMutt will ask to scan
948 through one or more mailboxes for Autocrypt: headers. Those headers are
949 then captured in the database as peer records and used for encryption.
950 If this is answered yes, they will be prompted for a mailbox. */
951 enum QuadOption scan = query_yesorno_ignore_macro(_("Scan a mailbox for autocrypt headers?"),
952 MUTT_YES);
953 while (scan == MUTT_YES)
954 {
955 struct Mailbox *m_cur = get_current_mailbox();
956 // L10N: The prompt for a mailbox to scan for Autocrypt: headers
957 if ((!mw_enter_fname(_("Scan mailbox"), folderbuf, true, m_cur, false, NULL,
958 NULL, MUTT_SEL_NO_FLAGS)) &&
959 (!buf_is_empty(folderbuf)))
960 {
961 expand_path(folderbuf, false);
962 struct Mailbox *m_ac = mx_path_resolve(buf_string(folderbuf));
963 /* NOTE: I am purposely *not* executing folder hooks here,
964 * as they can do all sorts of things like push into the getch() buffer.
965 * Authentication should be in account-hooks. */
966 if (mx_mbox_open(m_ac, MUTT_READONLY))
967 {
968 mx_mbox_close(m_ac);
969 }
970 buf_reset(folderbuf);
971 }
972
973 /* L10N: This is the second prompt to see if the user would like
974 to scan more than one mailbox for Autocrypt headers.
975 I'm purposely being extra verbose; asking first then prompting
976 for a mailbox. This is because this is a one-time operation
977 and I don't want them to accidentally ctrl-g and abort it. */
978 scan = query_yesorno_ignore_macro(_("Scan another mailbox for autocrypt headers?"), MUTT_YES);
979 }
980
981#ifdef USE_HCACHE
982 cs_subset_str_native_set(NeoMutt->sub, "header_cache", (intptr_t) old_hdrcache, NULL);
983 old_hdrcache = NULL;
984#endif
985 buf_pool_release(&folderbuf);
986}
#define MUTT_SEL_NO_FLAGS
No flags are set.
Definition lib.h:55
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:238
struct Mailbox * get_current_mailbox(void)
Get the current Mailbox.
Definition index.c:726
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
void expand_path(struct Buffer *buf, bool regex)
Create the canonical path.
Definition muttlib.c:122
bool mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
Open a mailbox and parse it.
Definition mx.c:285
struct Mailbox * mx_path_resolve(const char *path)
Get a Mailbox for a path.
Definition mx.c:1647
enum MxStatus mx_mbox_close(struct Mailbox *m)
Save changes and close mailbox.
Definition mx.c:595
#define MUTT_READONLY
Open in read-only mode.
Definition mxapi.h:42
QuadOption
Possible values for a quad-option.
Definition quad.h:36
A mailbox.
Definition mailbox.h:78
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:303
+ 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 434 of file db.c.

435{
436 int rc = -1;
437
439 sqlite3 *db = mod_data->autocrypt_db;
440 sqlite3_stmt *stmt = mod_data->account_delete_stmt;
441
442 if (!stmt)
443 {
444 if (sqlite3_prepare_v3(db,
445 "DELETE from account "
446 "WHERE email_addr = ?;",
447 -1, SQLITE_PREPARE_PERSISTENT, &stmt, NULL) != SQLITE_OK)
448 {
449 goto cleanup;
450 }
451 mod_data->account_delete_stmt = stmt;
452 }
453
454 if (sqlite3_bind_text(stmt, 1, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
455 goto cleanup;
456
457 if (sqlite3_step(stmt) != SQLITE_DONE)
458 goto cleanup;
459
460 rc = 0;
461
462cleanup:
463 sqlite3_reset(stmt);
464 return rc;
465}
@ MODULE_ID_AUTOCRYPT
ModuleAutocrypt, Autocrypt
Definition module_api.h:50
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:663
char * email_addr
Email address.
Definition lib.h:115
Autocrypt private Module data.
Definition module_data.h:32
sqlite3_stmt * account_delete_stmt
Delete an autocrypt account.
Definition module_data.h:39
sqlite3 * autocrypt_db
Autocrypt database.
Definition module_data.h:38
+ Here is the call graph for this function:
+ 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 240 of file db.c.

241{
242 if (!ptr || !*ptr)
243 return;
244
245 struct AutocryptAccount *ac = *ptr;
246 FREE(&ac->email_addr);
247 FREE(&ac->keyid);
248 FREE(&ac->keydata);
249 FREE(ptr);
250}
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
char * keydata
PGP Key data.
Definition lib.h:117
char * keyid
PGP Key id.
Definition lib.h:116
+ 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 259 of file db.c.

260{
261 int rc = -1;
262
264 sqlite3 *db = mod_data->autocrypt_db;
265 sqlite3_stmt *stmt = mod_data->account_get_stmt;
266
267 struct Address *norm_addr = copy_normalize_addr(addr);
268 *account = NULL;
269
270 if (!stmt)
271 {
272 if (sqlite3_prepare_v3(db,
273 "SELECT "
274 "email_addr, "
275 "keyid, "
276 "keydata, "
277 "prefer_encrypt, "
278 "enabled "
279 "FROM account "
280 "WHERE email_addr = ?",
281 -1, SQLITE_PREPARE_PERSISTENT, &stmt, NULL) != SQLITE_OK)
282 {
283 goto cleanup;
284 }
285 mod_data->account_get_stmt = stmt;
286 }
287
288 if (sqlite3_bind_text(stmt, 1, buf_string(norm_addr->mailbox), -1, SQLITE_STATIC) != SQLITE_OK)
289 {
290 goto cleanup;
291 }
292
293 int result = sqlite3_step(stmt);
294 if (result != SQLITE_ROW)
295 {
296 if (result == SQLITE_DONE)
297 rc = 0;
298 goto cleanup;
299 }
300
302 (*account)->email_addr = strdup_column_text(stmt, 0);
303 (*account)->keyid = strdup_column_text(stmt, 1);
304 (*account)->keydata = strdup_column_text(stmt, 2);
305 (*account)->prefer_encrypt = sqlite3_column_int(stmt, 3);
306 (*account)->enabled = sqlite3_column_int(stmt, 4);
307
308 rc = 1;
309
310cleanup:
311 mutt_addr_free(&norm_addr);
312 sqlite3_reset(stmt);
313 return rc;
314}
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:231
static struct Address * copy_normalize_addr(struct Address *addr)
Copy a normalised Email Address.
Definition db.c:199
static char * strdup_column_text(sqlite3_stmt *stmt, int index)
Copy a string from the database.
Definition db.c:221
sqlite3_stmt * account_get_stmt
Get the matching autocrypt accounts.
Definition module_data.h:40
+ 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 473 of file db.c.

474{
475 if (!aaa)
476 return -1;
477
478 int rc = -1;
479 sqlite3_stmt *stmt = NULL;
480
482 sqlite3 *db = mod_data->autocrypt_db;
483
484 /* Note, speed is not of the essence for the account management screen,
485 * so we don't bother with a persistent prepared statement */
486 if (sqlite3_prepare_v2(db,
487 "SELECT "
488 "email_addr, "
489 "keyid, "
490 "keydata, "
491 "prefer_encrypt, "
492 "enabled "
493 "FROM account "
494 "ORDER BY email_addr",
495 -1, &stmt, NULL) != SQLITE_OK)
496 {
497 goto cleanup;
498 }
499
500 int result = SQLITE_ERROR;
501 while ((result = sqlite3_step(stmt)) == SQLITE_ROW)
502 {
504
505 ac->email_addr = strdup_column_text(stmt, 0);
506 ac->keyid = strdup_column_text(stmt, 1);
507 ac->keydata = strdup_column_text(stmt, 2);
508 ac->prefer_encrypt = sqlite3_column_int(stmt, 3);
509 ac->enabled = sqlite3_column_int(stmt, 4);
510
511 ARRAY_ADD(aaa, ac);
512 }
513
514 if (result == SQLITE_DONE)
515 {
516 rc = ARRAY_SIZE(aaa);
517 }
518 else
519 {
520 struct AutocryptAccount **pac = NULL;
521 ARRAY_FOREACH(pac, aaa)
522 {
524 }
525 ARRAY_FREE(aaa);
526 }
527
528cleanup:
529 sqlite3_finalize(stmt);
530 return rc;
531}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:157
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:223
#define ARRAY_SIZE(head)
The number of elements stored.
Definition array.h:87
#define ARRAY_FREE(head)
Release all memory.
Definition array.h:209
bool enabled
Is this account enabled.
Definition lib.h:119
+ 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 325 of file db.c.

327{
328 int rc = -1;
329
331 sqlite3 *db = mod_data->autocrypt_db;
332 sqlite3_stmt *stmt = mod_data->account_insert_stmt;
333
334 struct Address *norm_addr = copy_normalize_addr(addr);
335
336 if (!stmt)
337 {
338 if (sqlite3_prepare_v3(db,
339 "INSERT INTO account "
340 "(email_addr, "
341 "keyid, "
342 "keydata, "
343 "prefer_encrypt, "
344 "enabled) "
345 "VALUES (?, ?, ?, ?, ?);",
346 -1, SQLITE_PREPARE_PERSISTENT, &stmt, NULL) != SQLITE_OK)
347 {
348 goto cleanup;
349 }
350 mod_data->account_insert_stmt = stmt;
351 }
352
353 if (sqlite3_bind_text(stmt, 1, buf_string(norm_addr->mailbox), -1, SQLITE_STATIC) != SQLITE_OK)
354 {
355 goto cleanup;
356 }
357 if (sqlite3_bind_text(stmt, 2, keyid, -1, SQLITE_STATIC) != SQLITE_OK)
358 goto cleanup;
359 if (sqlite3_bind_text(stmt, 3, keydata, -1, SQLITE_STATIC) != SQLITE_OK)
360 goto cleanup;
361 if (sqlite3_bind_int(stmt, 4, prefer_encrypt) != SQLITE_OK)
362 goto cleanup;
363 if (sqlite3_bind_int(stmt, 5, 1) != SQLITE_OK)
364 goto cleanup;
365
366 if (sqlite3_step(stmt) != SQLITE_DONE)
367 goto cleanup;
368
369 rc = 0;
370
371cleanup:
372 mutt_addr_free(&norm_addr);
373 sqlite3_reset(stmt);
374 return rc;
375}
sqlite3_stmt * account_insert_stmt
Insert a new autocrypt account.
Definition module_data.h:41
+ 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 231 of file db.c.

232{
233 return MUTT_MEM_CALLOC(1, struct AutocryptAccount);
234}
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
+ 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 383 of file db.c.

384{
385 int rc = -1;
386
388 sqlite3 *db = mod_data->autocrypt_db;
389 sqlite3_stmt *stmt = mod_data->account_update_stmt;
390
391 if (!stmt)
392 {
393 if (sqlite3_prepare_v3(db,
394 "UPDATE account SET "
395 "keyid = ?, "
396 "keydata = ?, "
397 "prefer_encrypt = ?, "
398 "enabled = ? "
399 "WHERE email_addr = ?;",
400 -1, SQLITE_PREPARE_PERSISTENT, &stmt, NULL) != SQLITE_OK)
401 {
402 goto cleanup;
403 }
404 mod_data->account_update_stmt = stmt;
405 }
406
407 if (sqlite3_bind_text(stmt, 1, acct->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
408 goto cleanup;
409 if (sqlite3_bind_text(stmt, 2, acct->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
410 goto cleanup;
411 if (sqlite3_bind_int(stmt, 3, acct->prefer_encrypt) != SQLITE_OK)
412 goto cleanup;
413 if (sqlite3_bind_int(stmt, 4, acct->enabled) != SQLITE_OK)
414 goto cleanup;
415 if (sqlite3_bind_text(stmt, 5, acct->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
416 goto cleanup;
417
418 if (sqlite3_step(stmt) != SQLITE_DONE)
419 goto cleanup;
420
421 rc = 0;
422
423cleanup:
424 sqlite3_reset(stmt);
425 return rc;
426}
sqlite3_stmt * account_update_stmt
Update an autocrypt account.
Definition module_data.h:42
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_autocrypt_db_close()

void mutt_autocrypt_db_close ( struct AutocryptModuleData * mod_data)

Close the Autocrypt SQLite database connection.

Parameters
mod_dataAutocrypt module data

Definition at line 127 of file db.c.

128{
129 if (!mod_data->autocrypt_db)
130 return;
131
132 sqlite3_finalize(mod_data->account_get_stmt);
133 mod_data->account_get_stmt = NULL;
134 sqlite3_finalize(mod_data->account_insert_stmt);
135 mod_data->account_insert_stmt = NULL;
136 sqlite3_finalize(mod_data->account_update_stmt);
137 mod_data->account_update_stmt = NULL;
138 sqlite3_finalize(mod_data->account_delete_stmt);
139 mod_data->account_delete_stmt = NULL;
140
141 sqlite3_finalize(mod_data->peer_get_stmt);
142 mod_data->peer_get_stmt = NULL;
143 sqlite3_finalize(mod_data->peer_insert_stmt);
144 mod_data->peer_insert_stmt = NULL;
145 sqlite3_finalize(mod_data->peer_update_stmt);
146 mod_data->peer_update_stmt = NULL;
147
148 sqlite3_finalize(mod_data->peer_history_insert_stmt);
149 mod_data->peer_history_insert_stmt = NULL;
150
151 sqlite3_finalize(mod_data->gossip_history_insert_stmt);
152 mod_data->gossip_history_insert_stmt = NULL;
153
154 sqlite3_close_v2(mod_data->autocrypt_db);
155 mod_data->autocrypt_db = NULL;
156}
sqlite3_stmt * peer_get_stmt
Get the matching peer addresses.
Definition module_data.h:44
sqlite3_stmt * peer_history_insert_stmt
Add to the peer history.
Definition module_data.h:45
sqlite3_stmt * peer_update_stmt
Update a peer address.
Definition module_data.h:47
sqlite3_stmt * gossip_history_insert_stmt
Add to the gossip history.
Definition module_data.h:43
sqlite3_stmt * peer_insert_stmt
Insert a new peer address.
Definition module_data.h:46
+ 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 863 of file db.c.

864{
865 if (!ptr || !*ptr)
866 return;
867
868 struct AutocryptGossipHistory *gh = *ptr;
869 FREE(&gh->peer_email_addr);
871 FREE(&gh->email_msgid);
872 FREE(&gh->gossip_keydata);
873 FREE(ptr);
874}
Autocrypt gossip history.
Definition lib.h:154
char * peer_email_addr
Email addressof the peer.
Definition lib.h:155
char * email_msgid
Sender's email's message id.
Definition lib.h:157
char * sender_email_addr
Sender's email address.
Definition lib.h:156
char * gossip_keydata
Gossip Key data.
Definition lib.h:159
+ 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 883 of file db.c.

885{
886 int rc = -1;
887
889 sqlite3 *db = mod_data->autocrypt_db;
890 sqlite3_stmt *stmt = mod_data->gossip_history_insert_stmt;
891
892 struct Address *norm_addr = copy_normalize_addr(addr);
893
894 if (!stmt)
895 {
896 if (sqlite3_prepare_v3(db,
897 "INSERT INTO gossip_history "
898 "(peer_email_addr, "
899 "sender_email_addr, "
900 "email_msgid, "
901 "timestamp, "
902 "gossip_keydata) "
903 "VALUES (?, ?, ?, ?, ?);",
904 -1, SQLITE_PREPARE_PERSISTENT, &stmt, NULL) != SQLITE_OK)
905 {
906 goto cleanup;
907 }
908 mod_data->gossip_history_insert_stmt = stmt;
909 }
910
911 if (sqlite3_bind_text(stmt, 1, buf_string(norm_addr->mailbox), -1, SQLITE_STATIC) != SQLITE_OK)
912 {
913 goto cleanup;
914 }
915 if (sqlite3_bind_text(stmt, 2, gossip_hist->sender_email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
916 {
917 goto cleanup;
918 }
919 if (sqlite3_bind_text(stmt, 3, gossip_hist->email_msgid, -1, SQLITE_STATIC) != SQLITE_OK)
920 {
921 goto cleanup;
922 }
923 if (sqlite3_bind_int64(stmt, 4, gossip_hist->timestamp) != SQLITE_OK)
924 goto cleanup;
925 if (sqlite3_bind_text(stmt, 5, gossip_hist->gossip_keydata, -1, SQLITE_STATIC) != SQLITE_OK)
926 {
927 goto cleanup;
928 }
929
930 if (sqlite3_step(stmt) != SQLITE_DONE)
931 goto cleanup;
932
933 rc = 0;
934
935cleanup:
936 mutt_addr_free(&norm_addr);
937 sqlite3_reset(stmt);
938 return rc;
939}
sqlite3_int64 timestamp
Timestamp of sender's email.
Definition lib.h:158
+ 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 854 of file db.c.

855{
856 return MUTT_MEM_CALLOC(1, struct AutocryptGossipHistory);
857}
+ 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 73 of file db.c.

74{
75 int rc = -1;
77
78 if (mod_data->autocrypt_db)
79 return 0;
80
81 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
82 const char *const c_autocrypt_dir = cs_subset_path(NeoMutt->sub, "autocrypt_dir");
83 if (!c_autocrypt || !c_autocrypt_dir)
84 return -1;
85
86 struct Buffer *db_path = buf_pool_get();
87 buf_concat_path(db_path, c_autocrypt_dir, "autocrypt.db");
88
89 struct stat st = { 0 };
90 if (stat(buf_string(db_path), &st) == 0)
91 {
92 sqlite3 *db = NULL;
93 if (sqlite3_open_v2(buf_string(db_path), &db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK)
94 {
95 /* L10N: autocrypt couldn't open the SQLite database.
96 The %s is the full path of the database file. */
97 mutt_error(_("Unable to open autocrypt database %s"), buf_string(db_path));
98 goto cleanup;
99 }
100 mod_data->autocrypt_db = db;
101
103 goto cleanup;
104 }
105 else
106 {
107 if (!can_create)
108 goto cleanup;
109 if (autocrypt_db_create(buf_string(db_path)))
110 goto cleanup;
111 /* Don't abort the whole init process because account creation failed */
114 }
115
116 rc = 0;
117
118cleanup:
119 buf_pool_release(&db_path);
120 return rc;
121}
static int autocrypt_db_create(const char *db_path)
Create an Autocrypt SQLite database.
Definition db.c:51
int mutt_autocrypt_schema_update(void)
Update the version number of the Autocrypt database schema.
Definition schema.c:111
int mutt_autocrypt_account_init(bool prompt)
Create a new Autocrypt account.
Definition autocrypt.c:150
void mutt_autocrypt_scan_mailboxes(void)
Scan mailboxes for Autocrypt headers.
Definition autocrypt.c:937
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 162 of file db.c.

163{
165 buf_lower(a->mailbox);
167}
bool mutt_addr_to_local(struct Address *a)
Convert an Address from Punycode.
Definition address.c:1349
bool mutt_addr_to_intl(struct Address *a)
Convert an Address to Punycode.
Definition address.c:1272
void buf_lower(struct Buffer *buf)
Sets a buffer to lowercase.
Definition buffer.c:734
+ 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 173 of file db.c.

174{
176
177 struct Address *np = NULL;
178 TAILQ_FOREACH(np, al, entries)
179 {
180 buf_lower(np->mailbox);
181 }
182
183 mutt_addrlist_to_intl(al, NULL);
184}
int mutt_addrlist_to_local(struct AddressList *al)
Convert an Address list from Punycode.
Definition address.c:1387
int mutt_addrlist_to_intl(struct AddressList *al, char **err)
Convert an Address list to Punycode.
Definition address.c:1302
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:782
+ 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 546 of file db.c.

547{
548 if (!ptr || !*ptr)
549 return;
550
551 struct AutocryptPeer *peer = *ptr;
552 FREE(&peer->email_addr);
553 FREE(&peer->keyid);
554 FREE(&peer->keydata);
555 FREE(&peer->gossip_keyid);
556 FREE(&peer->gossip_keydata);
557 FREE(ptr);
558}
Autocrypt peer.
Definition lib.h:127
char * gossip_keydata
Gossip Key data.
Definition lib.h:136
char * keyid
PGP Key id.
Definition lib.h:131
char * gossip_keyid
Gossip Key id.
Definition lib.h:135
char * keydata
PGP Key data.
Definition lib.h:132
char * email_addr
Email address.
Definition lib.h:128
+ 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 568 of file db.c.

569{
570 int rc = -1;
571
573 sqlite3 *db = mod_data->autocrypt_db;
574 sqlite3_stmt *stmt = mod_data->peer_get_stmt;
575
576 struct Address *norm_addr = copy_normalize_addr(addr);
577 *peer = NULL;
578
579 if (!stmt)
580 {
581 if (sqlite3_prepare_v3(db,
582 "SELECT "
583 "email_addr, "
584 "last_seen, "
585 "autocrypt_timestamp, "
586 "keyid, "
587 "keydata, "
588 "prefer_encrypt, "
589 "gossip_timestamp, "
590 "gossip_keyid, "
591 "gossip_keydata "
592 "FROM peer "
593 "WHERE email_addr = ?",
594 -1, SQLITE_PREPARE_PERSISTENT, &stmt, NULL) != SQLITE_OK)
595 {
596 goto cleanup;
597 }
598 mod_data->peer_get_stmt = stmt;
599 }
600
601 if (sqlite3_bind_text(stmt, 1, buf_string(norm_addr->mailbox), -1, SQLITE_STATIC) != SQLITE_OK)
602 {
603 goto cleanup;
604 }
605
606 int result = sqlite3_step(stmt);
607 if (result != SQLITE_ROW)
608 {
609 if (result == SQLITE_DONE)
610 rc = 0;
611 goto cleanup;
612 }
613
615 (*peer)->email_addr = strdup_column_text(stmt, 0);
616 (*peer)->last_seen = sqlite3_column_int64(stmt, 1);
617 (*peer)->autocrypt_timestamp = sqlite3_column_int64(stmt, 2);
618 (*peer)->keyid = strdup_column_text(stmt, 3);
619 (*peer)->keydata = strdup_column_text(stmt, 4);
620 (*peer)->prefer_encrypt = sqlite3_column_int(stmt, 5);
621 (*peer)->gossip_timestamp = sqlite3_column_int64(stmt, 6);
622 (*peer)->gossip_keyid = strdup_column_text(stmt, 7);
623 (*peer)->gossip_keydata = strdup_column_text(stmt, 8);
624
625 rc = 1;
626
627cleanup:
628 mutt_addr_free(&norm_addr);
629 sqlite3_reset(stmt);
630 return rc;
631}
struct AutocryptPeer * mutt_autocrypt_db_peer_new(void)
Create a new AutocryptPeer.
Definition db.c:537
+ 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 780 of file db.c.

781{
782 if (!ptr || !*ptr)
783 return;
784
785 struct AutocryptPeerHistory *ph = *ptr;
786 FREE(&ph->peer_email_addr);
787 FREE(&ph->email_msgid);
788 FREE(&ph->keydata);
789 FREE(ptr);
790}
Autocrypt peer history.
Definition lib.h:143
char * peer_email_addr
Email address of the peer.
Definition lib.h:144
char * email_msgid
Message id of the email.
Definition lib.h:145
char * keydata
PGP Key data.
Definition lib.h:147
+ 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 799 of file db.c.

801{
802 int rc = -1;
803
805 sqlite3 *db = mod_data->autocrypt_db;
806 sqlite3_stmt *stmt = mod_data->peer_history_insert_stmt;
807
808 struct Address *norm_addr = copy_normalize_addr(addr);
809
810 if (!stmt)
811 {
812 if (sqlite3_prepare_v3(db,
813 "INSERT INTO peer_history "
814 "(peer_email_addr, "
815 "email_msgid, "
816 "timestamp, "
817 "keydata) "
818 "VALUES (?, ?, ?, ?);",
819 -1, SQLITE_PREPARE_PERSISTENT, &stmt, NULL) != SQLITE_OK)
820 {
821 goto cleanup;
822 }
823 mod_data->peer_history_insert_stmt = stmt;
824 }
825
826 if (sqlite3_bind_text(stmt, 1, buf_string(norm_addr->mailbox), -1, SQLITE_STATIC) != SQLITE_OK)
827 {
828 goto cleanup;
829 }
830 if (sqlite3_bind_text(stmt, 2, peerhist->email_msgid, -1, SQLITE_STATIC) != SQLITE_OK)
831 {
832 goto cleanup;
833 }
834 if (sqlite3_bind_int64(stmt, 3, peerhist->timestamp) != SQLITE_OK)
835 goto cleanup;
836 if (sqlite3_bind_text(stmt, 4, peerhist->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
837 goto cleanup;
838
839 if (sqlite3_step(stmt) != SQLITE_DONE)
840 goto cleanup;
841
842 rc = 0;
843
844cleanup:
845 mutt_addr_free(&norm_addr);
846 sqlite3_reset(stmt);
847 return rc;
848}
sqlite3_int64 timestamp
Timestamp of email.
Definition lib.h:146
+ 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 771 of file db.c.

772{
773 return MUTT_MEM_CALLOC(1, struct AutocryptPeerHistory);
774}
+ 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 640 of file db.c.

641{
642 int rc = -1;
643 struct Address *norm_addr = NULL;
644
646 sqlite3 *db = mod_data->autocrypt_db;
647 sqlite3_stmt *stmt = mod_data->peer_insert_stmt;
648
649 norm_addr = copy_normalize_addr(addr);
650
651 if (!stmt)
652 {
653 if (sqlite3_prepare_v3(db,
654 "INSERT INTO peer "
655 "(email_addr, "
656 "last_seen, "
657 "autocrypt_timestamp, "
658 "keyid, "
659 "keydata, "
660 "prefer_encrypt, "
661 "gossip_timestamp, "
662 "gossip_keyid, "
663 "gossip_keydata) "
664 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);",
665 -1, SQLITE_PREPARE_PERSISTENT, &stmt, NULL) != SQLITE_OK)
666 {
667 goto cleanup;
668 }
669 mod_data->peer_insert_stmt = stmt;
670 }
671
672 if (sqlite3_bind_text(stmt, 1, buf_string(norm_addr->mailbox), -1, SQLITE_STATIC) != SQLITE_OK)
673 {
674 goto cleanup;
675 }
676 if (sqlite3_bind_int64(stmt, 2, peer->last_seen) != SQLITE_OK)
677 goto cleanup;
678 if (sqlite3_bind_int64(stmt, 3, peer->autocrypt_timestamp) != SQLITE_OK)
679 goto cleanup;
680 if (sqlite3_bind_text(stmt, 4, peer->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
681 goto cleanup;
682 if (sqlite3_bind_text(stmt, 5, peer->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
683 goto cleanup;
684 if (sqlite3_bind_int(stmt, 6, peer->prefer_encrypt) != SQLITE_OK)
685 goto cleanup;
686 if (sqlite3_bind_int64(stmt, 7, peer->gossip_timestamp) != SQLITE_OK)
687 goto cleanup;
688 if (sqlite3_bind_text(stmt, 8, peer->gossip_keyid, -1, SQLITE_STATIC) != SQLITE_OK)
689 goto cleanup;
690 if (sqlite3_bind_text(stmt, 9, peer->gossip_keydata, -1, SQLITE_STATIC) != SQLITE_OK)
691 goto cleanup;
692
693 if (sqlite3_step(stmt) != SQLITE_DONE)
694 goto cleanup;
695
696 rc = 0;
697
698cleanup:
699 mutt_addr_free(&norm_addr);
700 sqlite3_reset(stmt);
701 return rc;
702}
sqlite3_int64 autocrypt_timestamp
When the email was sent.
Definition lib.h:130
sqlite3_int64 last_seen
When was the peer last seen.
Definition lib.h:129
bool prefer_encrypt
false = nopref, true = mutual
Definition lib.h:133
sqlite3_int64 gossip_timestamp
Timestamp of Gossip header.
Definition lib.h:134
+ 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 537 of file db.c.

538{
539 return MUTT_MEM_CALLOC(1, struct AutocryptPeer);
540}
+ 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 710 of file db.c.

711{
712 int rc = -1;
713
715 sqlite3 *db = mod_data->autocrypt_db;
716 sqlite3_stmt *stmt = mod_data->peer_update_stmt;
717
718 if (!stmt)
719 {
720 if (sqlite3_prepare_v3(db,
721 "UPDATE peer SET "
722 "last_seen = ?, "
723 "autocrypt_timestamp = ?, "
724 "keyid = ?, "
725 "keydata = ?, "
726 "prefer_encrypt = ?, "
727 "gossip_timestamp = ?, "
728 "gossip_keyid = ?, "
729 "gossip_keydata = ? "
730 "WHERE email_addr = ?;",
731 -1, SQLITE_PREPARE_PERSISTENT, &stmt, NULL) != SQLITE_OK)
732 {
733 goto cleanup;
734 }
735 mod_data->peer_update_stmt = stmt;
736 }
737
738 if (sqlite3_bind_int64(stmt, 1, peer->last_seen) != SQLITE_OK)
739 goto cleanup;
740 if (sqlite3_bind_int64(stmt, 2, peer->autocrypt_timestamp) != SQLITE_OK)
741 goto cleanup;
742 if (sqlite3_bind_text(stmt, 3, peer->keyid, -1, SQLITE_STATIC) != SQLITE_OK)
743 goto cleanup;
744 if (sqlite3_bind_text(stmt, 4, peer->keydata, -1, SQLITE_STATIC) != SQLITE_OK)
745 goto cleanup;
746 if (sqlite3_bind_int(stmt, 5, peer->prefer_encrypt) != SQLITE_OK)
747 goto cleanup;
748 if (sqlite3_bind_int64(stmt, 6, peer->gossip_timestamp) != SQLITE_OK)
749 goto cleanup;
750 if (sqlite3_bind_text(stmt, 7, peer->gossip_keyid, -1, SQLITE_STATIC) != SQLITE_OK)
751 goto cleanup;
752 if (sqlite3_bind_text(stmt, 8, peer->gossip_keydata, -1, SQLITE_STATIC) != SQLITE_OK)
753 goto cleanup;
754 if (sqlite3_bind_text(stmt, 9, peer->email_addr, -1, SQLITE_STATIC) != SQLITE_OK)
755 goto cleanup;
756
757 if (sqlite3_step(stmt) != SQLITE_DONE)
758 goto cleanup;
759
760 rc = 0;
761
762cleanup:
763 sqlite3_reset(stmt);
764 return rc;
765}
+ Here is the call graph for this function:
+ 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 43 of file schema.c.

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

112{
113 sqlite3_stmt *stmt = NULL;
114 int rc = -1;
116 sqlite3 *db = mod_data->autocrypt_db;
117
118 if (sqlite3_prepare_v2(db, "SELECT version FROM schema;", -1, &stmt, NULL) != SQLITE_OK)
119 goto cleanup;
120
121 if (sqlite3_step(stmt) != SQLITE_ROW)
122 goto cleanup;
123
124 int version = sqlite3_column_int(stmt, 0);
125
126 if (version > 1)
127 {
128 /* L10N: The autocrypt database keeps track of schema version numbers.
129 This error occurs if the version number is too high.
130 Presumably because this is an old version of NeoMutt and the
131 database was upgraded by a future version. */
132 mutt_error(_("Autocrypt database version is too new"));
133 goto cleanup;
134 }
135
136 /* TODO: schema version upgrades go here.
137 * Bump one by one, each update inside a transaction. */
138
139 rc = 0;
140
141cleanup:
142 sqlite3_finalize(stmt);
143 return rc;
144}
+ Here is the call graph for this function:
+ 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:1059
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 NUL-terminated base64 string to raw bytes.
Definition base64.c:261
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() -.
+ 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.
bool OptAutocryptGpgme
(pseudo) use Autocrypt context inside ncrypt/crypt_gpgme.c
Definition globals.c:44
+ 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:62
#define FALLTHROUGH
Definition lib.h:117
enum QuadOption query_yesorno(const char *prompt, enum QuadOption def)
Ask the user a Yes/No question.
Definition question.c:329
+ 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 145 of file dlg_autocrypt.c.

146{
147 struct AutocryptData *ad = menu->mdata;
148
149 // Clear out any existing data
151 menu->max = 0;
152
153 struct AutocryptAccountArray accounts = ARRAY_HEAD_INITIALIZER;
154
155 if (mutt_autocrypt_db_account_get_all(&accounts) < 0)
156 return false;
157
158 menu->max = ARRAY_SIZE(&accounts);
159
160 struct AutocryptAccount **pac = NULL;
161 ARRAY_FOREACH(pac, &accounts)
162 {
163 struct AccountEntry *entry = MUTT_MEM_CALLOC(1, struct AccountEntry);
164
165 entry->num = ARRAY_FOREACH_IDX_pac + 1;
166 /* note: we are transferring the account pointer to the entries
167 * array, and freeing the accounts array below. the account
168 * will be freed in autocrypt_menu_free(). */
169 entry->account = *pac;
170
171 entry->addr = mutt_addr_new();
172 entry->addr->mailbox = buf_new((*pac)->email_addr);
173 mutt_addr_to_local(entry->addr);
174 ARRAY_ADD(&ad->entries, entry);
175 }
176 ARRAY_FREE(&accounts);
177
179 return true;
180}
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:473
void account_entry_array_clear(struct AccountEntryArray *entries)
Clear an AccountEntry array.
#define MENU_REDRAW_FULL
Redraw everything.
Definition lib.h:60
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition menu.c:179
An entry in the Autocrypt account Menu.
Definition private.h:45
struct Address * addr
Email address associated with the account.
Definition private.h:48
struct AutocryptAccount * account
Account details.
Definition private.h:47
int num
Number in the index.
Definition private.h:46
Data to pass to the Autocrypt Functions.
struct Menu * menu
Autocrypt Menu.
struct AccountEntryArray entries
Account Entries.
void * mdata
Private data.
Definition lib.h:149
int max
Number of entries in the menu.
Definition lib.h:82
+ Here is the call graph for this function:
+ Here is the caller graph for this function: