From a2252cf4dfbe09157dd9f014bd1c4dcdad030142 Mon Sep 17 00:00:00 2001 From: Kevin McCarthy Date: Thu, 10 Aug 2017 18:18:26 -0700 Subject: [PATCH] Fix attachment check_traditional and extract_keys operations. (see #3728) Add helpers and iterate over the actx->idx instead of the BODY structure. --- ncrypt/crypt_gpgme.c | 16 +++++++------- ncrypt/crypt_gpgme.h | 2 +- ncrypt/crypt_mod.h | 2 +- ncrypt/crypt_mod_pgp_classic.c | 4 ++-- ncrypt/crypt_mod_pgp_gpgme.c | 4 ++-- ncrypt/cryptglue.c | 4 ++-- ncrypt/ncrypt.h | 2 +- ncrypt/pgp.c | 16 +++++++------- recvattach.c | 38 +++++++++++++++++++++++++++++----- 9 files changed, 58 insertions(+), 30 deletions(-) diff --git a/ncrypt/crypt_gpgme.c b/ncrypt/crypt_gpgme.c index c7e879864..e08cb15c8 100644 --- a/ncrypt/crypt_gpgme.c +++ b/ncrypt/crypt_gpgme.c @@ -2281,7 +2281,7 @@ static int line_compare(const char *a, size_t n, const char *b) /* * Implementation of `pgp_check_traditional'. */ -static int pgp_check_traditional_one_body(FILE *fp, struct Body *b, int tagged_only) +static int pgp_check_traditional_one_body(FILE *fp, struct Body *b) { char tempfile[_POSIX_PATH_MAX]; char buf[HUGE_STRING]; @@ -2293,9 +2293,6 @@ static int pgp_check_traditional_one_body(FILE *fp, struct Body *b, int tagged_o if (b->type != TYPETEXT) return 0; - if (tagged_only && !b->tagged) - return 0; - mutt_mktemp(tempfile, sizeof(tempfile)); if (mutt_decode_save_attachment(fp, b, tempfile, 0, 0) != 0) { @@ -2339,21 +2336,24 @@ static int pgp_check_traditional_one_body(FILE *fp, struct Body *b, int tagged_o return 1; } -int pgp_gpgme_check_traditional(FILE *fp, struct Body *b, int tagged_only) +int pgp_gpgme_check_traditional(FILE *fp, struct Body *b, int just_one) { int rv = 0; int r; for (; b; b = b->next) { - if (is_multipart(b)) - rv = (pgp_gpgme_check_traditional(fp, b->parts, tagged_only) || rv); + if (!just_one && is_multipart(b)) + rv = (pgp_gpgme_check_traditional(fp, b->parts, 0) || rv); else if (b->type == TYPETEXT) { if ((r = mutt_is_application_pgp(b))) rv = (rv || r); else - rv = (pgp_check_traditional_one_body(fp, b, tagged_only) || rv); + rv = (pgp_check_traditional_one_body(fp, b) || rv); } + + if (just_one) + break; } return rv; } diff --git a/ncrypt/crypt_gpgme.h b/ncrypt/crypt_gpgme.h index 9faabf105..3b5c32296 100644 --- a/ncrypt/crypt_gpgme.h +++ b/ncrypt/crypt_gpgme.h @@ -42,7 +42,7 @@ struct Body *smime_gpgme_build_smime_entity(struct Body *a, char *keylist); int pgp_gpgme_decrypt_mime(FILE *fpin, FILE **fpout, struct Body *b, struct Body **cur); int smime_gpgme_decrypt_mime(FILE *fpin, FILE **fpout, struct Body *b, struct Body **cur); -int pgp_gpgme_check_traditional(FILE *fp, struct Body *b, int tagged_only); +int pgp_gpgme_check_traditional (FILE *fp, struct Body *b, int just_one); void pgp_gpgme_invoke_import(const char *fname); int pgp_gpgme_application_handler(struct Body *m, struct State *s); diff --git a/ncrypt/crypt_mod.h b/ncrypt/crypt_mod.h index c9133290b..26bd2cc17 100644 --- a/ncrypt/crypt_mod.h +++ b/ncrypt/crypt_mod.h @@ -44,7 +44,7 @@ typedef int (*crypt_func_application_handler_t)(struct Body *m, struct State *s) typedef int (*crypt_func_encrypted_handler_t)(struct Body *m, struct State *s); typedef void (*crypt_func_pgp_invoke_getkeys_t)(struct Address *addr); -typedef int (*crypt_func_pgp_check_traditional_t)(FILE *fp, struct Body *b, int tagged_only); +typedef int (*crypt_func_pgp_check_traditional_t) (FILE *fp, struct Body *b, int just_one); typedef struct Body *(*crypt_func_pgp_traditional_encryptsign_t)(struct Body *a, int flags, char *keylist); typedef struct Body *(*crypt_func_pgp_make_key_attachment_t)(char *tempf); typedef char *(*crypt_func_findkeys_t)(struct Address *adrlist, int oppenc_mode); diff --git a/ncrypt/crypt_mod_pgp_classic.c b/ncrypt/crypt_mod_pgp_classic.c index cd04688b4..0ff1cee1e 100644 --- a/ncrypt/crypt_mod_pgp_classic.c +++ b/ncrypt/crypt_mod_pgp_classic.c @@ -87,9 +87,9 @@ static struct Body *crypt_mod_pgp_make_key_attachment(char *tempf) return pgp_make_key_attachment(tempf); } -static int crypt_mod_pgp_check_traditional(FILE *fp, struct Body *b, int tagged_only) +static int crypt_mod_pgp_check_traditional(FILE *fp, struct Body *b, int just_one) { - return pgp_check_traditional(fp, b, tagged_only); + return pgp_check_traditional(fp, b, just_one); } static struct Body *crypt_mod_pgp_traditional_encryptsign(struct Body *a, diff --git a/ncrypt/crypt_mod_pgp_gpgme.c b/ncrypt/crypt_mod_pgp_gpgme.c index bd805539e..0045747e6 100644 --- a/ncrypt/crypt_mod_pgp_gpgme.c +++ b/ncrypt/crypt_mod_pgp_gpgme.c @@ -66,9 +66,9 @@ static int crypt_mod_pgp_encrypted_handler(struct Body *m, struct State *s) return pgp_gpgme_encrypted_handler(m, s); } -static int crypt_mod_pgp_check_traditional(FILE *fp, struct Body *b, int tagged_only) +static int crypt_mod_pgp_check_traditional(FILE *fp, struct Body *b, int just_one) { - return pgp_gpgme_check_traditional(fp, b, tagged_only); + return pgp_gpgme_check_traditional(fp, b, just_one); } static void crypt_mod_pgp_invoke_import(const char *fname) diff --git a/ncrypt/cryptglue.c b/ncrypt/cryptglue.c index 19bc64deb..138108707 100644 --- a/ncrypt/cryptglue.c +++ b/ncrypt/cryptglue.c @@ -189,10 +189,10 @@ void crypt_pgp_invoke_getkeys(struct Address *addr) /** * crypt_pgp_check_traditional - Check for a traditional PGP message in body B */ -int crypt_pgp_check_traditional(FILE *fp, struct Body *b, int tagged_only) +int crypt_pgp_check_traditional(FILE *fp, struct Body *b, int just_one) { if (CRYPT_MOD_CALL_CHECK(PGP, pgp_check_traditional)) - return (CRYPT_MOD_CALL(PGP, pgp_check_traditional))(fp, b, tagged_only); + return (CRYPT_MOD_CALL(PGP, pgp_check_traditional))(fp, b, just_one); return 0; } diff --git a/ncrypt/ncrypt.h b/ncrypt/ncrypt.h index 0eb1fc75b..2815811af 100644 --- a/ncrypt/ncrypt.h +++ b/ncrypt/ncrypt.h @@ -126,7 +126,7 @@ int crypt_pgp_decrypt_mime(FILE *a, FILE **b, struct Body *c, struct Body **d); int crypt_pgp_application_pgp_handler(struct Body *m, struct State *s); int crypt_pgp_encrypted_handler(struct Body *a, struct State *s); void crypt_pgp_invoke_getkeys(struct Address *addr); -int crypt_pgp_check_traditional(FILE *fp, struct Body *b, int tagged_only); +int crypt_pgp_check_traditional (FILE *fp, struct Body *b, int just_one); struct Body *crypt_pgp_make_key_attachment(char *tempf); int crypt_pgp_send_menu(struct Header *msg); void crypt_pgp_extract_keys_from_attachment_list(FILE *fp, int tag, struct Body *top); diff --git a/ncrypt/pgp.c b/ncrypt/pgp.c index b111ecb9f..8dd89a047 100644 --- a/ncrypt/pgp.c +++ b/ncrypt/pgp.c @@ -620,7 +620,7 @@ out: return rc; } -static int pgp_check_traditional_one_body(FILE *fp, struct Body *b, int tagged_only) +static int pgp_check_traditional_one_body(FILE *fp, struct Body *b) { char tempfile[_POSIX_PATH_MAX]; char buf[HUGE_STRING]; @@ -633,9 +633,6 @@ static int pgp_check_traditional_one_body(FILE *fp, struct Body *b, int tagged_o if (b->type != TYPETEXT) return 0; - if (tagged_only && !b->tagged) - return 0; - mutt_mktemp(tempfile, sizeof(tempfile)); if (mutt_decode_save_attachment(fp, b, tempfile, 0, 0) != 0) { @@ -680,21 +677,24 @@ static int pgp_check_traditional_one_body(FILE *fp, struct Body *b, int tagged_o return 1; } -int pgp_check_traditional(FILE *fp, struct Body *b, int tagged_only) +int pgp_check_traditional(FILE *fp, struct Body *b, int just_one) { int rv = 0; int r; for (; b; b = b->next) { - if (is_multipart(b)) - rv = pgp_check_traditional(fp, b->parts, tagged_only) || rv; + if (!just_one && is_multipart(b)) + rv = pgp_check_traditional(fp, b->parts, 0) || rv; else if (b->type == TYPETEXT) { if ((r = mutt_is_application_pgp(b))) rv = rv || r; else - rv = pgp_check_traditional_one_body(fp, b, tagged_only) || rv; + rv = pgp_check_traditional_one_body(fp, b) || rv; } + + if (just_one) + break; } return rv; diff --git a/recvattach.c b/recvattach.c index 8a3d67bb4..9fca1b201 100644 --- a/recvattach.c +++ b/recvattach.c @@ -818,6 +818,37 @@ void mutt_print_attachment_list(struct AttachCtx *actx, FILE *fp, int tag, struc print_attachment_list(actx, fp, tag, top, &state); } +static void recvattach_extract_pgp_keys(struct AttachCtx *actx, struct Menu *menu) +{ + int i; + + if (!menu->tagprefix) + crypt_pgp_extract_keys_from_attachment_list(CURATTACH->fp, 0, CURATTACH->content); + else + { + for (i = 0; i < actx->idxlen; i++) + if (actx->idx[i]->content->tagged) + crypt_pgp_extract_keys_from_attachment_list(actx->idx[i]->fp, 0, + actx->idx[i]->content); + } +} + +static int recvattach_pgp_check_traditional(struct AttachCtx *actx, struct Menu *menu) +{ + int i, rv = 0; + + if (!menu->tagprefix) + rv = crypt_pgp_check_traditional(CURATTACH->fp, CURATTACH->content, 1); + else + { + for (i = 0; i < actx->idxlen; i++) + if (actx->idx[i]->content->tagged) + rv = rv || crypt_pgp_check_traditional(actx->idx[i]->fp, actx->idx[i]->content, 1); + } + + return rv; +} + int mutt_attach_display_loop(struct Menu *menu, int op, struct Header *hdr, struct AttachCtx *actx, int recv) { @@ -1130,16 +1161,13 @@ void mutt_view_attachments(struct Header *hdr) case OP_EXTRACT_KEYS: if ((WithCrypto & APPLICATION_PGP)) { - crypt_pgp_extract_keys_from_attachment_list( - fp, menu->tagprefix, menu->tagprefix ? cur : CURATTACH->content); + recvattach_extract_pgp_keys(actx, menu); menu->redraw = REDRAW_FULL; } break; case OP_CHECK_TRADITIONAL: - if ((WithCrypto & APPLICATION_PGP) && - crypt_pgp_check_traditional(fp, menu->tagprefix ? cur : CURATTACH->content, - menu->tagprefix)) + if ((WithCrypto & APPLICATION_PGP) && recvattach_pgp_check_traditional(actx, menu)) { hdr->security = crypt_query(cur); menu->redraw = REDRAW_FULL; -- 2.40.0