NeoMutt  2024-04-25-85-g27bab4
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
pgp_make_key_attachment()

Generate a public key attachment. More...

+ Collaboration diagram for pgp_make_key_attachment():

Functions

struct Bodypgp_gpgme_make_key_attachment (void)
 Generate a public key attachment - Implements CryptModuleSpecs::pgp_make_key_attachment() -.
 
struct Bodypgp_class_make_key_attachment (void)
 Generate a public key attachment - Implements CryptModuleSpecs::pgp_make_key_attachment() -.
 

Detailed Description

Generate a public key attachment.

Return values
ptrNew Body containing the attachment
NULLError

Function Documentation

◆ pgp_gpgme_make_key_attachment()

struct Body * pgp_gpgme_make_key_attachment ( void  )

Generate a public key attachment - Implements CryptModuleSpecs::pgp_make_key_attachment() -.

Definition at line 3766 of file crypt_gpgme.c.

3767{
3768 gpgme_ctx_t ctx = NULL;
3769 gpgme_key_t export_keys[2] = { 0 };
3770 gpgme_data_t keydata = NULL;
3771 struct Body *att = NULL;
3772 char buf[1024] = { 0 };
3773
3774 OptPgpCheckTrust = false;
3775
3776 struct CryptKeyInfo *key = crypt_ask_for_key(_("Please enter the key ID: "), NULL,
3778 if (!key)
3779 goto bail;
3780 export_keys[0] = key->kobj;
3781 export_keys[1] = NULL;
3782
3783 ctx = create_gpgme_context(false);
3784 gpgme_set_armor(ctx, 1);
3785 keydata = create_gpgme_data();
3786 gpgme_error_t err = gpgme_op_export_keys(ctx, export_keys, 0, keydata);
3787 if (err != GPG_ERR_NO_ERROR)
3788 {
3789 mutt_error(_("Error exporting key: %s"), gpgme_strerror(err));
3790 goto bail;
3791 }
3792
3793 char *tempf = data_object_to_tempfile(keydata, NULL);
3794 if (!tempf)
3795 goto bail;
3796
3797 att = mutt_body_new();
3798 /* tempf is a newly allocated string, so this is correct: */
3799 att->filename = tempf;
3800 att->unlink = true;
3801 att->use_disp = false;
3802 att->type = TYPE_APPLICATION;
3803 att->subtype = mutt_str_dup("pgp-keys");
3804 /* L10N: MIME description for exported (attached) keys.
3805 You can translate this entry to a non-ASCII string (it will be encoded),
3806 but it may be safer to keep it untranslated. */
3807 snprintf(buf, sizeof(buf), _("PGP Key 0x%s"), crypt_keyid(key));
3808 att->description = mutt_str_dup(buf);
3810
3811 att->length = mutt_file_get_size(tempf);
3812
3813bail:
3814 crypt_key_free(&key);
3815 gpgme_data_release(keydata);
3816 gpgme_release(ctx);
3817
3818 return att;
3819}
static gpgme_data_t create_gpgme_data(void)
Create a new GPGME data object.
Definition: crypt_gpgme.c:400
gpgme_ctx_t create_gpgme_context(bool for_smime)
Create a new GPGME context.
Definition: crypt_gpgme.c:361
static char * data_object_to_tempfile(gpgme_data_t data, FILE **fp_ret)
Copy a data object to a temporary file.
Definition: crypt_gpgme.c:563
static struct CryptKeyInfo * crypt_ask_for_key(const char *tag, const char *whatfor, KeyFlags abilities, unsigned int app, bool *forced_valid)
Ask the user for a key.
Definition: crypt_gpgme.c:3465
const char * crypt_keyid(struct CryptKeyInfo *k)
Find the ID for the key.
Definition: crypt_gpgme.c:138
static void crypt_key_free(struct CryptKeyInfo **keylist)
Release all the keys in a list.
Definition: crypt_gpgme.c:253
struct Body * mutt_body_new(void)
Create a new Body.
Definition: body.c:44
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition: file.c:1519
bool OptPgpCheckTrust
(pseudo) used by dlg_pgp()
Definition: globals.c:73
#define mutt_error(...)
Definition: logging2.h:92
@ TYPE_APPLICATION
Type: 'application/*'.
Definition: mime.h:33
#define _(a)
Definition: message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition: lib.h:90
#define KEYFLAG_NO_FLAGS
No flags are set.
Definition: lib.h:126
void mutt_update_encoding(struct Body *b, struct ConfigSubset *sub)
Update the encoding type.
Definition: sendlib.c:422
The body of an email.
Definition: body.h:36
bool unlink
If true, filename should be unlink()ed before free()ing this structure.
Definition: body.h:67
LOFF_T length
length (in bytes) of attachment
Definition: body.h:53
bool use_disp
Content-Disposition uses filename= ?
Definition: body.h:47
char * description
content-description
Definition: body.h:55
char * subtype
content-type subtype
Definition: body.h:60
unsigned int type
content-type primary type, ContentType
Definition: body.h:40
char * filename
When sending a message, this is the file to which this structure refers.
Definition: body.h:58
A stored PGP key.
Definition: crypt_gpgme.h:44
gpgme_key_t kobj
Definition: crypt_gpgme.h:46
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
+ Here is the call graph for this function:

◆ pgp_class_make_key_attachment()

struct Body * pgp_class_make_key_attachment ( void  )

Generate a public key attachment - Implements CryptModuleSpecs::pgp_make_key_attachment() -.

Definition at line 259 of file pgpkey.c.

260{
261 struct Body *att = NULL;
262 char buf[1024] = { 0 };
263 char tmp[256] = { 0 };
264 struct stat st = { 0 };
265 pid_t pid;
266 OptPgpCheckTrust = false;
267 struct Buffer *tempf = NULL;
268
269 struct PgpKeyInfo *key = pgp_ask_for_key(_("Please enter the key ID: "), NULL,
271
272 if (!key)
273 return NULL;
274
275 snprintf(tmp, sizeof(tmp), "0x%s", pgp_fpr_or_lkeyid(pgp_principal_key(key)));
276 pgp_key_free(&key);
277
278 tempf = buf_pool_get();
279 buf_mktemp(tempf);
280 FILE *fp_tmp = mutt_file_fopen(buf_string(tempf), "w");
281 if (!fp_tmp)
282 {
283 mutt_perror(_("Can't create temporary file"));
284 goto cleanup;
285 }
286
287 FILE *fp_null = mutt_file_fopen("/dev/null", "w");
288 if (!fp_null)
289 {
290 mutt_perror(_("Can't open /dev/null"));
291 mutt_file_fclose(&fp_tmp);
292 unlink(buf_string(tempf));
293 goto cleanup;
294 }
295
296 mutt_message(_("Invoking PGP..."));
297
298 pid = pgp_invoke_export(NULL, NULL, NULL, -1, fileno(fp_tmp), fileno(fp_null), tmp);
299 if (pid == -1)
300 {
301 mutt_perror(_("Can't create filter"));
302 unlink(buf_string(tempf));
303 mutt_file_fclose(&fp_tmp);
304 mutt_file_fclose(&fp_null);
305 goto cleanup;
306 }
307
308 filter_wait(pid);
309
310 mutt_file_fclose(&fp_tmp);
311 mutt_file_fclose(&fp_null);
312
313 att = mutt_body_new();
314 att->filename = buf_strdup(tempf);
315 att->unlink = true;
316 att->use_disp = false;
317 att->type = TYPE_APPLICATION;
318 att->subtype = mutt_str_dup("pgp-keys");
319 snprintf(buf, sizeof(buf), _("PGP Key %s"), tmp);
320 att->description = mutt_str_dup(buf);
322
323 stat(buf_string(tempf), &st);
324 att->length = st.st_size;
325
326cleanup:
327 buf_pool_release(&tempf);
328 return att;
329}
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:571
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
#define mutt_file_fclose(FP)
Definition: file.h:149
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:148
#define mutt_message(...)
Definition: logging2.h:91
#define mutt_perror(...)
Definition: logging2.h:93
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:220
char * pgp_fpr_or_lkeyid(struct PgpKeyInfo *k)
Get the fingerprint or long keyid.
Definition: pgp.c:234
pid_t pgp_invoke_export(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err, int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, const char *uids)
Use PGP to export a key from the user's keyring.
Definition: pgpinvoke.c:442
struct PgpKeyInfo * pgp_ask_for_key(char *tag, const char *whatfor, KeyFlags abilities, enum PgpRing keyring)
Ask the user for a PGP key.
Definition: pgpkey.c:199
struct PgpKeyInfo * pgp_principal_key(struct PgpKeyInfo *key)
Get the main (parent) PGP key.
Definition: pgpkey.c:92
@ PGP_PUBRING
Public keys.
Definition: pgpkey.h:39
void pgp_key_free(struct PgpKeyInfo **kpp)
Free a PGP key info.
Definition: pgplib.c:201
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
String manipulation buffer.
Definition: buffer.h:36
Information about a PGP key.
Definition: pgplib.h:47
#define buf_mktemp(buf)
Definition: tmp.h:33
+ Here is the call graph for this function: