lib: convert notmuch decryption policy to an enum

Future patches in this series will introduce new policies; this merely
readies the way for them.

We also convert --try-decrypt to a keyword argument instead of a boolean.
This commit is contained in:
Daniel Kahn Gillmor 2017-12-08 01:23:52 -05:00 committed by David Bremner
parent b62045a186
commit 798aa789b5
10 changed files with 58 additions and 35 deletions

View file

@ -525,7 +525,7 @@ _index_encrypted_mime_part (notmuch_message_t *message,
notmuch_database_t * notmuch = NULL; notmuch_database_t * notmuch = NULL;
GMimeObject *clear = NULL; GMimeObject *clear = NULL;
if (!indexopts || !notmuch_indexopts_get_decrypt_policy (indexopts)) if (!indexopts || (notmuch_indexopts_get_decrypt_policy (indexopts) == NOTMUCH_DECRYPT_FALSE))
return; return;
notmuch = _notmuch_message_database (message); notmuch = _notmuch_message_database (message);

View file

@ -26,25 +26,26 @@ notmuch_database_get_default_indexopts (notmuch_database_t *db)
notmuch_indexopts_t *ret = talloc_zero (db, notmuch_indexopts_t); notmuch_indexopts_t *ret = talloc_zero (db, notmuch_indexopts_t);
if (!ret) if (!ret)
return ret; return ret;
ret->crypto.decrypt = NOTMUCH_DECRYPT_FALSE;
char * decrypt; char * decrypt_policy;
notmuch_status_t err = notmuch_database_get_config (db, "index.decrypt", &decrypt); notmuch_status_t err = notmuch_database_get_config (db, "index.decrypt", &decrypt_policy);
if (err) if (err)
return ret; return ret;
if (decrypt && if (decrypt_policy &&
((!(strcasecmp(decrypt, "true"))) || ((!(strcasecmp(decrypt_policy, "true"))) ||
(!(strcasecmp(decrypt, "yes"))) || (!(strcasecmp(decrypt_policy, "yes"))) ||
(!(strcasecmp(decrypt, "1"))))) (!(strcasecmp(decrypt_policy, "1")))))
notmuch_indexopts_set_decrypt_policy (ret, true); notmuch_indexopts_set_decrypt_policy (ret, NOTMUCH_DECRYPT_TRUE);
free (decrypt); free (decrypt_policy);
return ret; return ret;
} }
notmuch_status_t notmuch_status_t
notmuch_indexopts_set_decrypt_policy (notmuch_indexopts_t *indexopts, notmuch_indexopts_set_decrypt_policy (notmuch_indexopts_t *indexopts,
notmuch_bool_t decrypt_policy) notmuch_decryption_policy_t decrypt_policy)
{ {
if (!indexopts) if (!indexopts)
return NOTMUCH_STATUS_NULL_POINTER; return NOTMUCH_STATUS_NULL_POINTER;
@ -52,7 +53,7 @@ notmuch_indexopts_set_decrypt_policy (notmuch_indexopts_t *indexopts,
return NOTMUCH_STATUS_SUCCESS; return NOTMUCH_STATUS_SUCCESS;
} }
notmuch_bool_t notmuch_decryption_policy_t
notmuch_indexopts_get_decrypt_policy (const notmuch_indexopts_t *indexopts) notmuch_indexopts_get_decrypt_policy (const notmuch_indexopts_t *indexopts)
{ {
if (!indexopts) if (!indexopts)

View file

@ -2233,6 +2233,16 @@ notmuch_config_list_destroy (notmuch_config_list_t *config_list);
notmuch_indexopts_t * notmuch_indexopts_t *
notmuch_database_get_default_indexopts (notmuch_database_t *db); notmuch_database_get_default_indexopts (notmuch_database_t *db);
/**
* Stating a policy about how to decrypt messages.
*
* See index.decrypt in notmuch-config(1) for more details.
*/
typedef enum {
NOTMUCH_DECRYPT_FALSE,
NOTMUCH_DECRYPT_TRUE,
} notmuch_decryption_policy_t;
/** /**
* Specify whether to decrypt encrypted parts while indexing. * Specify whether to decrypt encrypted parts while indexing.
* *
@ -2245,7 +2255,7 @@ notmuch_database_get_default_indexopts (notmuch_database_t *db);
*/ */
notmuch_status_t notmuch_status_t
notmuch_indexopts_set_decrypt_policy (notmuch_indexopts_t *indexopts, notmuch_indexopts_set_decrypt_policy (notmuch_indexopts_t *indexopts,
notmuch_bool_t decrypt_policy); notmuch_decryption_policy_t decrypt_policy);
/** /**
* Return whether to decrypt encrypted parts while indexing. * Return whether to decrypt encrypted parts while indexing.
@ -2253,7 +2263,7 @@ notmuch_indexopts_set_decrypt_policy (notmuch_indexopts_t *indexopts,
* *
* @since libnotmuch 5.1 (notmuch 0.26) * @since libnotmuch 5.1 (notmuch 0.26)
*/ */
notmuch_bool_t notmuch_decryption_policy_t
notmuch_indexopts_get_decrypt_policy (const notmuch_indexopts_t *indexopts); notmuch_indexopts_get_decrypt_policy (const notmuch_indexopts_t *indexopts);
/** /**

View file

@ -270,7 +270,7 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)
} }
#if (GMIME_MAJOR_VERSION < 3) #if (GMIME_MAJOR_VERSION < 3)
if ((GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto->decrypt) if ((GMIME_IS_MULTIPART_ENCRYPTED (part) && (node->ctx->crypto->decrypt == NOTMUCH_DECRYPT_TRUE))
|| (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify)) { || (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify)) {
GMimeContentType *content_type = g_mime_object_get_content_type (part); GMimeContentType *content_type = g_mime_object_get_content_type (part);
const char *protocol = g_mime_content_type_get_parameter (content_type, "protocol"); const char *protocol = g_mime_content_type_get_parameter (content_type, "protocol");
@ -286,7 +286,7 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)
#endif #endif
/* Handle PGP/MIME parts */ /* Handle PGP/MIME parts */
if (GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto->decrypt) { if (GMIME_IS_MULTIPART_ENCRYPTED (part) && (node->ctx->crypto->decrypt == NOTMUCH_DECRYPT_TRUE)) {
if (node->nchildren != 2) { if (node->nchildren != 2) {
/* this violates RFC 3156 section 4, so we won't bother with it. */ /* this violates RFC 3156 section 4, so we won't bother with it. */
fprintf (stderr, "Error: %d part(s) for a multipart/encrypted " fprintf (stderr, "Error: %d part(s) for a multipart/encrypted "

View file

@ -414,9 +414,10 @@ struct mime_node {
/* Construct a new MIME node pointing to the root message part of /* Construct a new MIME node pointing to the root message part of
* message. If crypto->verify is true, signed child parts will be * message. If crypto->verify is true, signed child parts will be
* verified. If crypto->decrypt is true, encrypted child parts will be * verified. If crypto->decrypt is NOTMUCH_DECRYPT_TRUE, encrypted
* decrypted. If the crypto contexts (crypto->gpgctx or * child parts will be decrypted. If the crypto contexts
* crypto->pkcs7) are NULL, they will be lazily initialized. * (crypto->gpgctx or crypto->pkcs7) are NULL, they will be lazily
* initialized.
* *
* Return value: * Return value:
* *
@ -500,7 +501,7 @@ int notmuch_minimal_options (const char* subcommand_name,
/* the state chosen by the user invoking one of the notmuch /* the state chosen by the user invoking one of the notmuch
* subcommands that does indexing */ * subcommands that does indexing */
struct _notmuch_client_indexing_cli_choices { struct _notmuch_client_indexing_cli_choices {
bool decrypt_policy; int decrypt_policy;
bool decrypt_policy_set; bool decrypt_policy_set;
notmuch_indexopts_t * opts; notmuch_indexopts_t * opts;
}; };

View file

@ -700,9 +700,11 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])
int opt_index; int opt_index;
notmuch_show_params_t params = { notmuch_show_params_t params = {
.part = -1, .part = -1,
.crypto = { .decrypt = NOTMUCH_DECRYPT_FALSE },
}; };
int format = FORMAT_DEFAULT; int format = FORMAT_DEFAULT;
int reply_all = true; int reply_all = true;
bool decrypt = false;
notmuch_opt_desc_t options[] = { notmuch_opt_desc_t options[] = {
{ .opt_keyword = &format, .name = "format", .keywords = { .opt_keyword = &format, .name = "format", .keywords =
@ -716,7 +718,7 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])
(notmuch_keyword_t []){ { "all", true }, (notmuch_keyword_t []){ { "all", true },
{ "sender", false }, { "sender", false },
{ 0, 0 } } }, { 0, 0 } } },
{ .opt_bool = &params.crypto.decrypt, .name = "decrypt" }, { .opt_bool = &decrypt, .name = "decrypt" },
{ .opt_inherit = notmuch_shared_options }, { .opt_inherit = notmuch_shared_options },
{ } { }
}; };
@ -726,6 +728,8 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])
return EXIT_FAILURE; return EXIT_FAILURE;
notmuch_process_shared_options (argv[0]); notmuch_process_shared_options (argv[0]);
if (decrypt)
params.crypto.decrypt = NOTMUCH_DECRYPT_TRUE;
notmuch_exit_if_unsupported_format (); notmuch_exit_if_unsupported_format ();

View file

@ -1083,11 +1083,13 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
.part = -1, .part = -1,
.omit_excluded = true, .omit_excluded = true,
.output_body = true, .output_body = true,
.crypto = { .decrypt = NOTMUCH_DECRYPT_FALSE },
}; };
int format = NOTMUCH_FORMAT_NOT_SPECIFIED; int format = NOTMUCH_FORMAT_NOT_SPECIFIED;
bool exclude = true; bool exclude = true;
bool entire_thread_set = false; bool entire_thread_set = false;
bool single_message; bool single_message;
bool decrypt = false;
notmuch_opt_desc_t options[] = { notmuch_opt_desc_t options[] = {
{ .opt_keyword = &format, .name = "format", .keywords = { .opt_keyword = &format, .name = "format", .keywords =
@ -1102,7 +1104,7 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
{ .opt_bool = &params.entire_thread, .name = "entire-thread", { .opt_bool = &params.entire_thread, .name = "entire-thread",
.present = &entire_thread_set }, .present = &entire_thread_set },
{ .opt_int = &params.part, .name = "part" }, { .opt_int = &params.part, .name = "part" },
{ .opt_bool = &params.crypto.decrypt, .name = "decrypt" }, { .opt_bool = &decrypt, .name = "decrypt" },
{ .opt_bool = &params.crypto.verify, .name = "verify" }, { .opt_bool = &params.crypto.verify, .name = "verify" },
{ .opt_bool = &params.output_body, .name = "body" }, { .opt_bool = &params.output_body, .name = "body" },
{ .opt_bool = &params.include_html, .name = "include-html" }, { .opt_bool = &params.include_html, .name = "include-html" },
@ -1116,9 +1118,11 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
notmuch_process_shared_options (argv[0]); notmuch_process_shared_options (argv[0]);
/* decryption implies verification */ if (decrypt) {
if (params.crypto.decrypt) params.crypto.decrypt = NOTMUCH_DECRYPT_TRUE;
/* decryption implies verification */
params.crypto.verify = true; params.crypto.verify = true;
}
/* specifying a part implies single message display */ /* specifying a part implies single message display */
single_message = params.part >= 0; single_message = params.part >= 0;

View file

@ -99,8 +99,11 @@ int notmuch_minimal_options (const char *subcommand_name,
struct _notmuch_client_indexing_cli_choices indexing_cli_choices = { }; struct _notmuch_client_indexing_cli_choices indexing_cli_choices = { };
const notmuch_opt_desc_t notmuch_shared_indexing_options [] = { const notmuch_opt_desc_t notmuch_shared_indexing_options [] = {
{ .opt_bool = &indexing_cli_choices.decrypt_policy, { .opt_keyword = &indexing_cli_choices.decrypt_policy,
.present = &indexing_cli_choices.decrypt_policy_set, .present = &indexing_cli_choices.decrypt_policy_set, .keywords =
(notmuch_keyword_t []){ { "false", NOTMUCH_DECRYPT_FALSE },
{ "true", NOTMUCH_DECRYPT_TRUE },
{ 0, 0 } },
.name = "decrypt" }, .name = "decrypt" },
{ } { }
}; };
@ -117,15 +120,15 @@ notmuch_process_shared_indexing_options (notmuch_database_t *notmuch, g_mime_3_u
return NOTMUCH_STATUS_OUT_OF_MEMORY; return NOTMUCH_STATUS_OUT_OF_MEMORY;
status = notmuch_indexopts_set_decrypt_policy (indexing_cli_choices.opts, indexing_cli_choices.decrypt_policy); status = notmuch_indexopts_set_decrypt_policy (indexing_cli_choices.opts, indexing_cli_choices.decrypt_policy);
if (status != NOTMUCH_STATUS_SUCCESS) { if (status != NOTMUCH_STATUS_SUCCESS) {
fprintf (stderr, "Error: Failed to set index decryption policy to %s. (%s)\n", fprintf (stderr, "Error: Failed to set index decryption policy to %d. (%s)\n",
indexing_cli_choices.decrypt_policy ? "True" : "False", notmuch_status_to_string (status)); indexing_cli_choices.decrypt_policy, notmuch_status_to_string (status));
notmuch_indexopts_destroy (indexing_cli_choices.opts); notmuch_indexopts_destroy (indexing_cli_choices.opts);
indexing_cli_choices.opts = NULL; indexing_cli_choices.opts = NULL;
return status; return status;
} }
} }
#if (GMIME_MAJOR_VERSION < 3) #if (GMIME_MAJOR_VERSION < 3)
if (indexing_cli_choices.opts && notmuch_indexopts_get_decrypt_policy (indexing_cli_choices.opts)) { if (indexing_cli_choices.opts && notmuch_indexopts_get_decrypt_policy (indexing_cli_choices.opts) == NOTMUCH_DECRYPT_TRUE) {
const char* gpg_path = notmuch_config_get_crypto_gpg_path (config); const char* gpg_path = notmuch_config_get_crypto_gpg_path (config);
if (gpg_path && strcmp(gpg_path, "gpg")) if (gpg_path && strcmp(gpg_path, "gpg"))
fprintf (stderr, "Warning: deprecated crypto.gpg_path is set to '%s'\n" fprintf (stderr, "Warning: deprecated crypto.gpg_path is set to '%s'\n"

View file

@ -71,8 +71,8 @@ test_expect_equal \
# try reinserting it with decryption, should appear again, but now we # try reinserting it with decryption, should appear again, but now we
# have two copies of the message: # have two copies of the message:
test_begin_subtest "message cleartext is present after reinserting with --decrypt" test_begin_subtest "message cleartext is present after reinserting with --decrypt=true"
notmuch insert --folder=sent --decrypt <<<"$contents" notmuch insert --folder=sent --decrypt=true <<<"$contents"
output=$(notmuch search wumpus) output=$(notmuch search wumpus)
expected='thread:0000000000000003 2000-01-01 [1/1(2)] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox unread)' expected='thread:0000000000000003 2000-01-01 [1/1(2)] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox unread)'
test_expect_equal \ test_expect_equal \
@ -93,8 +93,8 @@ test_expect_equal \
# try inserting it with decryption, should appear as a single copy # try inserting it with decryption, should appear as a single copy
# (note: i think thread id skips 4 because of duplicate message-id # (note: i think thread id skips 4 because of duplicate message-id
# insertion, above) # insertion, above)
test_begin_subtest "message cleartext is present with insert --decrypt" test_begin_subtest "message cleartext is present with insert --decrypt=true"
notmuch insert --folder=sent --decrypt <<<"$contents" notmuch insert --folder=sent --decrypt=true <<<"$contents"
output=$(notmuch search wumpus) output=$(notmuch search wumpus)
expected='thread:0000000000000005 2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox unread)' expected='thread:0000000000000005 2000-01-01 [1/1] Notmuch Test Suite; test encrypted message for cleartext index 002 (encrypted inbox unread)'
test_expect_equal \ test_expect_equal \
@ -159,7 +159,7 @@ test_expect_equal \
add_email_corpus crypto add_email_corpus crypto
test_begin_subtest "indexing message fails when secret key not available" test_begin_subtest "indexing message fails when secret key not available"
notmuch reindex --decrypt id:simple-encrypted@crypto.notmuchmail.org notmuch reindex --decrypt=true id:simple-encrypted@crypto.notmuchmail.org
output=$(notmuch dump ) output=$(notmuch dump )
expected='#notmuch-dump batch-tag:3 config,properties,tags expected='#notmuch-dump batch-tag:3 config,properties,tags
+encrypted +inbox +unread -- id:simple-encrypted@crypto.notmuchmail.org +encrypted +inbox +unread -- id:simple-encrypted@crypto.notmuchmail.org
@ -180,7 +180,7 @@ notmuch restore <<EOF
#notmuch-dump batch-tag:3 config,properties,tags #notmuch-dump batch-tag:3 config,properties,tags
#= simple-encrypted@crypto.notmuchmail.org session-key=9%3AFC09987F5F927CC0CC0EE80A96E4C5BBF4A499818FB591207705DFDDD6112CF9 #= simple-encrypted@crypto.notmuchmail.org session-key=9%3AFC09987F5F927CC0CC0EE80A96E4C5BBF4A499818FB591207705DFDDD6112CF9
EOF EOF
notmuch reindex --decrypt id:simple-encrypted@crypto.notmuchmail.org notmuch reindex --decrypt=true id:simple-encrypted@crypto.notmuchmail.org
output=$(notmuch search sekrit) output=$(notmuch search sekrit)
expected='thread:0000000000000001 2016-12-22 [1/1] Daniel Kahn Gillmor; encrypted message (encrypted inbox unread)' expected='thread:0000000000000001 2016-12-22 [1/1] Daniel Kahn Gillmor; encrypted message (encrypted inbox unread)'
if [ $NOTMUCH_HAVE_GMIME_SESSION_KEYS -eq 0 ]; then if [ $NOTMUCH_HAVE_GMIME_SESSION_KEYS -eq 0 ]; then

View file

@ -7,7 +7,7 @@
typedef struct _notmuch_crypto { typedef struct _notmuch_crypto {
bool verify; bool verify;
bool decrypt; notmuch_decryption_policy_t decrypt;
#if (GMIME_MAJOR_VERSION < 3) #if (GMIME_MAJOR_VERSION < 3)
GMimeCryptoContext* gpgctx; GMimeCryptoContext* gpgctx;
GMimeCryptoContext* pkcs7ctx; GMimeCryptoContext* pkcs7ctx;