Commit ecbba07c authored by Kevin J. McCarthy's avatar Kevin J. McCarthy

Refactor the address list generation out of the find_keys routines.

All four find_keys routines have a similar set up code for generating a
single address list out of the to, cc, bcc lists.  This patch pulls all
the code into crypt_get_keys.

This is done to simplify the functions before later patches make them
more complicated (with the oppenc_mode parameter).
parent 2539e02d
......@@ -4306,44 +4306,21 @@ static crypt_key_t *crypt_ask_for_key (char *tag,
/* This routine attempts to find the keyids of the recipients of a
message. It returns NULL if any of the keys can not be found. */
static char *find_keys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc,
unsigned int app)
static char *find_keys (ADDRESS *adrlist, unsigned int app)
{
char *keyID, *keylist = NULL, *t;
size_t keylist_size = 0;
size_t keylist_used = 0;
ADDRESS *tmp = NULL, *addr = NULL;
ADDRESS **last = &tmp;
ADDRESS *addr = NULL;
ADDRESS *p, *q;
int i;
crypt_key_t *k_info, *key;
const char *fqdn = mutt_fqdn (1);
#if 0
*r_application = APPLICATION_PGP|APPLICATION_SMIME;
#endif
for (i = 0; i < 3; i++)
{
switch (i)
{
case 0: p = to; break;
case 1: p = cc; break;
case 2: p = bcc; break;
default: abort ();
}
*last = rfc822_cpy_adr (p, 0);
while (*last)
last = &((*last)->next);
}
if (fqdn)
rfc822_qualify (tmp, fqdn);
tmp = mutt_remove_duplicates (tmp);
for (p = tmp; p ; p = p->next)
for (p = adrlist; p ; p = p->next)
{
char buf[LONG_STRING];
int forced_valid = 0;
......@@ -4380,7 +4357,6 @@ static char *find_keys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc,
else if (r == -1)
{
FREE (&keylist);
rfc822_free_address (&tmp);
rfc822_free_address (&addr);
return NULL;
}
......@@ -4402,7 +4378,6 @@ static char *find_keys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc,
&forced_valid)) == NULL)
{
FREE (&keylist);
rfc822_free_address (&tmp);
rfc822_free_address (&addr);
return NULL;
}
......@@ -4431,18 +4406,17 @@ static char *find_keys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc,
crypt_free_key (&key);
rfc822_free_address (&addr);
}
rfc822_free_address (&tmp);
return (keylist);
}
char *pgp_gpgme_findkeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
char *pgp_gpgme_findkeys (ADDRESS *adrlist)
{
return find_keys (to, cc, bcc, APPLICATION_PGP);
return find_keys (adrlist, APPLICATION_PGP);
}
char *smime_gpgme_findkeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
char *smime_gpgme_findkeys (ADDRESS *adrlist)
{
return find_keys (to, cc, bcc, APPLICATION_SMIME);
return find_keys (adrlist, APPLICATION_SMIME);
}
#ifdef HAVE_GPGME_OP_EXPORT_KEYS
......
......@@ -24,8 +24,8 @@
void pgp_gpgme_init (void);
void smime_gpgme_init (void);
char *pgp_gpgme_findkeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc);
char *smime_gpgme_findkeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc);
char *pgp_gpgme_findkeys (ADDRESS *adrlist);
char *smime_gpgme_findkeys (ADDRESS *adrlist);
BODY *pgp_gpgme_encrypt_message (BODY *a, char *keylist, int sign);
BODY *smime_gpgme_build_smime_entity (BODY *a, char *keylist);
......
......@@ -46,9 +46,9 @@ static int crypt_mod_pgp_application_handler (BODY *m, STATE *s)
return pgp_application_pgp_handler (m, s);
}
static char *crypt_mod_pgp_findkeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
static char *crypt_mod_pgp_findkeys (ADDRESS *adrlist)
{
return pgp_findKeys (to, cc, bcc);
return pgp_findKeys (adrlist);
}
static BODY *crypt_mod_pgp_sign_message (BODY *a)
......
......@@ -70,9 +70,9 @@ static void crypt_mod_pgp_invoke_import (const char *fname)
pgp_gpgme_invoke_import (fname);
}
static char *crypt_mod_pgp_findkeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
static char *crypt_mod_pgp_findkeys (ADDRESS *adrlist)
{
return pgp_gpgme_findkeys (to, cc, bcc);
return pgp_gpgme_findkeys (adrlist);
}
static BODY *crypt_mod_pgp_sign_message (BODY *a)
......
......@@ -46,9 +46,9 @@ static int crypt_mod_smime_application_handler (BODY *m, STATE *s)
return smime_application_smime_handler (m, s);
}
static char *crypt_mod_smime_findkeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
static char *crypt_mod_smime_findkeys (ADDRESS *adrlist)
{
return smime_findKeys (to, cc, bcc);
return smime_findKeys (adrlist);
}
static BODY *crypt_mod_smime_sign_message (BODY *a)
......
......@@ -55,9 +55,9 @@ static int crypt_mod_smime_application_handler (BODY *m, STATE *s)
return smime_gpgme_application_handler (m, s);
}
static char *crypt_mod_smime_findkeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
static char *crypt_mod_smime_findkeys (ADDRESS *adrlist)
{
return smime_gpgme_findkeys (to, cc, bcc);
return smime_gpgme_findkeys (adrlist);
}
static BODY *crypt_mod_smime_sign_message (BODY *a)
......
......@@ -43,8 +43,7 @@ typedef int (*crypt_func_pgp_check_traditional_t) (FILE *fp, BODY *b,
typedef BODY *(*crypt_func_pgp_traditional_encryptsign_t) (BODY *a, int flags,
char *keylist);
typedef BODY *(*crypt_func_pgp_make_key_attachment_t) (char *tempf);
typedef char *(*crypt_func_findkeys_t) (ADDRESS *to,
ADDRESS *cc, ADDRESS *bcc);
typedef char *(*crypt_func_findkeys_t) (ADDRESS *adrlist);
typedef BODY *(*crypt_func_sign_message_t) (BODY *a);
typedef BODY *(*crypt_func_pgp_encrypt_message_t) (BODY *a, char *keylist,
int sign);
......
......@@ -709,6 +709,9 @@ void crypt_extract_keys_from_messages (HEADER * h)
int crypt_get_keys (HEADER *msg, char **keylist)
{
ADDRESS *adrlist = NULL, *last = NULL;
const char *fqdn = mutt_fqdn (1);
/* Do a quick check to make sure that we can find all of the encryption
* keys if the user has requested this service.
*/
......@@ -719,6 +722,14 @@ int crypt_get_keys (HEADER *msg, char **keylist)
if ((WithCrypto & APPLICATION_PGP))
set_option (OPTPGPCHECKTRUST);
last = rfc822_append (&adrlist, msg->env->to, 0);
last = rfc822_append (last ? &last : &adrlist, msg->env->cc, 0);
rfc822_append (last ? &last : &adrlist, msg->env->bcc, 0);
if (fqdn)
rfc822_qualify (adrlist, fqdn);
adrlist = mutt_remove_duplicates (adrlist);
*keylist = NULL;
if (msg->security & ENCRYPT)
......@@ -726,19 +737,25 @@ int crypt_get_keys (HEADER *msg, char **keylist)
if ((WithCrypto & APPLICATION_PGP)
&& (msg->security & APPLICATION_PGP))
{
if ((*keylist = crypt_pgp_findkeys (msg->env->to, msg->env->cc,
msg->env->bcc)) == NULL)
if ((*keylist = crypt_pgp_findkeys (adrlist)) == NULL)
{
rfc822_free_address (&adrlist);
return (-1);
}
unset_option (OPTPGPCHECKTRUST);
}
if ((WithCrypto & APPLICATION_SMIME)
&& (msg->security & APPLICATION_SMIME))
{
if ((*keylist = crypt_smime_findkeys (msg->env->to, msg->env->cc,
msg->env->bcc)) == NULL)
if ((*keylist = crypt_smime_findkeys (adrlist)) == NULL)
{
rfc822_free_address (&adrlist);
return (-1);
}
}
}
rfc822_free_address (&adrlist);
return (0);
}
......
......@@ -200,10 +200,10 @@ BODY *crypt_pgp_make_key_attachment (char *tempf)
/* This routine attempts to find the keyids of the recipients of a
message. It returns NULL if any of the keys can not be found. */
char *crypt_pgp_findkeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
char *crypt_pgp_findkeys (ADDRESS *adrlist)
{
if (CRYPT_MOD_CALL_CHECK (PGP, findkeys))
return (CRYPT_MOD_CALL (PGP, findkeys)) (to, cc, bcc);
return (CRYPT_MOD_CALL (PGP, findkeys)) (adrlist);
return NULL;
}
......@@ -334,10 +334,10 @@ int crypt_smime_verify_sender(HEADER *h)
/* This routine attempts to find the keyids of the recipients of a
message. It returns NULL if any of the keys can not be found. */
char *crypt_smime_findkeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
char *crypt_smime_findkeys (ADDRESS *adrlist)
{
if (CRYPT_MOD_CALL_CHECK (SMIME, findkeys))
return (CRYPT_MOD_CALL (SMIME, findkeys)) (to, cc, bcc);
return (CRYPT_MOD_CALL (SMIME, findkeys)) (adrlist);
return NULL;
}
......
......@@ -211,7 +211,7 @@ BODY *crypt_pgp_make_key_attachment (char *tempf);
/* This routine attempts to find the keyids of the recipients of a
message. It returns NULL if any of the keys can not be found. */
char *crypt_pgp_findkeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc);
char *crypt_pgp_findkeys (ADDRESS *adrlist);
/* Create a new body with a PGP signed message from A. */
BODY *crypt_pgp_sign_message (BODY *a);
......@@ -260,7 +260,7 @@ char *crypt_smime_ask_for_key (char *prompt, char *mailbox, short public);
/* This routine attempts to find the keyids of the recipients of a
message. It returns NULL if any of the keys can not be found. */
char *crypt_smime_findkeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc);
char *crypt_smime_findkeys (ADDRESS *adrlist);
/* fixme: Needs documentation. */
BODY *crypt_smime_sign_message (BODY *a);
......
......@@ -1183,40 +1183,18 @@ static short is_numerical_keyid (const char *s)
/* This routine attempts to find the keyids of the recipients of a message.
* It returns NULL if any of the keys can not be found.
*/
char *pgp_findKeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
char *pgp_findKeys (ADDRESS *adrlist)
{
char *keyID, *keylist = NULL;
size_t keylist_size = 0;
size_t keylist_used = 0;
ADDRESS *tmp = NULL, *addr = NULL;
ADDRESS **last = &tmp;
ADDRESS *addr = NULL;
ADDRESS *p, *q;
int i;
pgp_key_t k_info = NULL, key = NULL;
const char *fqdn = mutt_fqdn (1);
for (i = 0; i < 3; i++)
{
switch (i)
{
case 0: p = to; break;
case 1: p = cc; break;
case 2: p = bcc; break;
default: abort ();
}
*last = rfc822_cpy_adr (p, 0);
while (*last)
last = &((*last)->next);
}
if (fqdn)
rfc822_qualify (tmp, fqdn);
tmp = mutt_remove_duplicates (tmp);
for (p = tmp; p ; p = p->next)
for (p = adrlist; p ; p = p->next)
{
char buf[LONG_STRING];
......@@ -1249,7 +1227,6 @@ char *pgp_findKeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
else if (r == -1)
{
FREE (&keylist);
rfc822_free_address (&tmp);
rfc822_free_address (&addr);
return NULL;
}
......@@ -1266,7 +1243,6 @@ char *pgp_findKeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
KEYFLAG_CANENCRYPT, PGP_PUBRING)) == NULL)
{
FREE (&keylist);
rfc822_free_address (&tmp);
rfc822_free_address (&addr);
return NULL;
}
......@@ -1287,7 +1263,6 @@ char *pgp_findKeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
rfc822_free_address (&addr);
}
rfc822_free_address (&tmp);
return (keylist);
}
......
......@@ -51,7 +51,7 @@ pgp_key_t pgp_get_candidates (pgp_ring_t, LIST *);
pgp_key_t pgp_getkeybyaddr (ADDRESS *, short, pgp_ring_t);
pgp_key_t pgp_getkeybystr (char *, short, pgp_ring_t);
char *pgp_findKeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc);
char *pgp_findKeys (ADDRESS *adrlist);
void pgp_forget_passphrase (void);
int pgp_application_pgp_handler (BODY *, STATE *);
......
......@@ -731,39 +731,14 @@ void smime_getkeys (ENVELOPE *env)
* It returns NULL if any of the keys can not be found.
*/
char *smime_findKeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
char *smime_findKeys (ADDRESS *adrlist)
{
char *keyID, *keylist = NULL;
size_t keylist_size = 0;
size_t keylist_used = 0;
ADDRESS *tmp = NULL, *addr = NULL;
ADDRESS **last = &tmp;
ADDRESS *p, *q;
int i;
const char *fqdn = mutt_fqdn (1);
for (i = 0; i < 3; i++)
{
switch (i)
{
case 0: p = to; break;
case 1: p = cc; break;
case 2: p = bcc; break;
default: abort ();
}
*last = rfc822_cpy_adr (p, 0);
while (*last)
last = &((*last)->next);
}
if (fqdn)
rfc822_qualify (tmp, fqdn);
tmp = mutt_remove_duplicates (tmp);
for (p = tmp; p ; p = p->next)
for (p = adrlist; p ; p = p->next)
{
char buf[LONG_STRING];
......@@ -780,8 +755,6 @@ char *smime_findKeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
{
mutt_message (_("No (valid) certificate found for %s."), q->mailbox);
FREE (&keylist);
rfc822_free_address (&tmp);
rfc822_free_address (&addr);
return NULL;
}
......@@ -790,10 +763,7 @@ char *smime_findKeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc)
sprintf (keylist + keylist_used, "%s\n", keyID); /* __SPRINTF_CHECKED__ */
keylist_used = mutt_strlen (keylist);
rfc822_free_address (&addr);
}
rfc822_free_address (&tmp);
return (keylist);
}
......
......@@ -50,7 +50,7 @@ void smime_getkeys (ENVELOPE *);
char* smime_ask_for_key (char *, char *, short);
char *smime_findKeys (ADDRESS *to, ADDRESS *cc, ADDRESS *bcc);
char *smime_findKeys (ADDRESS *adrlist);
void smime_invoke_import (char *, char *);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment