NeoMutt  2025-12-11-1040-g5187ba
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
pgp_traditional_encryptsign()

Create an inline PGP encrypted, signed email. More...

Collaboration diagram for pgp_traditional_encryptsign():

Functions

struct Bodypgp_class_traditional_encryptsign (struct Body *b, SecurityFlags flags, char *keylist)
 Create an inline PGP encrypted, signed email - Implements CryptModuleSpecs::pgp_traditional_encryptsign() -.

Detailed Description

Create an inline PGP encrypted, signed email.

Parameters
bBody of the email
flagsFlags, see SecurityFlags
keylistList of keys to encrypt to (space-separated)
Return values
ptrNew encrypted/signed Body
NULLError

Function Documentation

◆ pgp_class_traditional_encryptsign()

struct Body * pgp_class_traditional_encryptsign ( struct Body * b,
SecurityFlags flags,
char * keylist )

Create an inline PGP encrypted, signed email - Implements CryptModuleSpecs::pgp_traditional_encryptsign() -.

Definition at line 1754 of file pgp.c.

1755{
1757 struct Body *b_enc = NULL;
1758 char body_charset[256] = { 0 };
1759 const char *from_charset = NULL;
1760 const char *send_charset = NULL;
1761 bool empty = false;
1762 bool err;
1763 char buf[256] = { 0 };
1764 pid_t pid;
1765 struct Buffer *pgpinfile = buf_pool_get();
1766 struct Buffer *pgpoutfile = buf_pool_get();
1767
1768 if (b->type != TYPE_TEXT)
1769 goto cleanup;
1770 if (!mutt_istr_equal(b->subtype, "plain"))
1771 goto cleanup;
1772
1773 FILE *fp_body = mutt_file_fopen(b->filename, "r");
1774 if (!fp_body)
1775 {
1776 mutt_perror("%s", b->filename);
1777 goto cleanup;
1778 }
1779
1780 buf_mktemp(pgpinfile);
1781 FILE *fp_pgp_in = mutt_file_fopen(buf_string(pgpinfile), "w");
1782 if (!fp_pgp_in)
1783 {
1784 mutt_perror("%s", buf_string(pgpinfile));
1785 mutt_file_fclose(&fp_body);
1786 goto cleanup;
1787 }
1788
1789 /* The following code is really correct: If noconv is set,
1790 * b's charset parameter contains the on-disk character set, and
1791 * we have to convert from that to utf-8. If noconv is not set,
1792 * we have to convert from $charset to utf-8. */
1793
1794 mutt_body_get_charset(b, body_charset, sizeof(body_charset));
1795 if (b->noconv)
1796 from_charset = body_charset;
1797 else
1798 from_charset = cc_charset();
1799
1800 if (mutt_ch_is_us_ascii(body_charset))
1801 {
1802 send_charset = "us-ascii";
1803 mutt_file_copy_stream(fp_body, fp_pgp_in);
1804 }
1805 else
1806 {
1807 int c;
1808 struct FgetConv *fc = NULL;
1809
1810 if (flags & SEC_ENCRYPT)
1811 send_charset = "us-ascii";
1812 else
1813 send_charset = "utf-8";
1814
1815 /* fromcode is assumed to be correct: we set flags to 0 */
1816 fc = mutt_ch_fgetconv_open(fp_body, from_charset, "utf-8", MUTT_ICONV_NONE);
1817 while ((c = mutt_ch_fgetconv(fc)) != EOF)
1818 fputc(c, fp_pgp_in);
1819
1821 }
1822 mutt_file_fclose(&fp_body);
1823 mutt_file_fclose(&fp_pgp_in);
1824
1825 buf_mktemp(pgpoutfile);
1826 FILE *fp_pgp_out = mutt_file_fopen(buf_string(pgpoutfile), "w+");
1827 FILE *fp_pgp_err = mutt_file_mkstemp();
1828 if (!fp_pgp_out || !fp_pgp_err)
1829 {
1830 mutt_perror("%s", fp_pgp_out ? "Can't create temporary file" : buf_string(pgpoutfile));
1831 unlink(buf_string(pgpinfile));
1832 if (fp_pgp_out)
1833 {
1834 mutt_file_fclose(&fp_pgp_out);
1835 unlink(buf_string(pgpoutfile));
1836 }
1837 mutt_file_fclose(&fp_pgp_err);
1838 goto cleanup;
1839 }
1840
1841 pid = pgp_invoke_traditional(&fp_pgp_in, NULL, NULL, -1, fileno(fp_pgp_out),
1842 fileno(fp_pgp_err), buf_string(pgpinfile), keylist, flags);
1843 if (pid == -1)
1844 {
1845 mutt_perror(_("Can't invoke PGP"));
1846 mutt_file_fclose(&fp_pgp_out);
1847 mutt_file_fclose(&fp_pgp_err);
1848 mutt_file_unlink(buf_string(pgpinfile));
1849 unlink(buf_string(pgpoutfile));
1850 goto cleanup;
1851 }
1852
1853 if (pgp_use_gpg_agent())
1854 *mod_data->pgp_pass = '\0';
1855 if (flags & SEC_SIGN)
1856 fprintf(fp_pgp_in, "%s\n", mod_data->pgp_pass);
1857 mutt_file_fclose(&fp_pgp_in);
1858
1859 const bool c_pgp_check_exit = cs_subset_bool(NeoMutt->sub, "pgp_check_exit");
1860 if (filter_wait(pid) && c_pgp_check_exit)
1861 empty = true;
1862
1863 mutt_file_unlink(buf_string(pgpinfile));
1864
1865 fflush(fp_pgp_out);
1866 fflush(fp_pgp_err);
1867
1868 fseek(fp_pgp_out, 0, SEEK_SET);
1869 clearerr(fp_pgp_out);
1870 fseek(fp_pgp_err, 0, SEEK_SET);
1871 clearerr(fp_pgp_err);
1872
1873 if (!empty)
1874 empty = (fgetc(fp_pgp_out) == EOF);
1875 mutt_file_fclose(&fp_pgp_out);
1876
1877 err = false;
1878
1879 while (fgets(buf, sizeof(buf), fp_pgp_err))
1880 {
1881 err = true;
1882 fputs(buf, stdout);
1883 }
1884
1885 mutt_file_fclose(&fp_pgp_err);
1886
1887 if (err)
1889
1890 if (empty)
1891 {
1892 if (flags & SEC_SIGN)
1893 pgp_class_void_passphrase(); /* just in case */
1894 unlink(buf_string(pgpoutfile));
1895 goto cleanup;
1896 }
1897
1898 b_enc = mutt_body_new();
1899
1900 b_enc->encoding = ENC_7BIT;
1901
1902 b_enc->type = TYPE_TEXT;
1903 b_enc->subtype = mutt_str_dup("plain");
1904
1905 mutt_param_set(&b_enc->parameter, "x-action",
1906 (flags & SEC_ENCRYPT) ? "pgp-encrypted" : "pgp-signed");
1907 mutt_param_set(&b_enc->parameter, "charset", send_charset);
1908
1909 b_enc->filename = buf_strdup(pgpoutfile);
1910
1911 b_enc->disposition = DISP_NONE;
1912 b_enc->unlink = true;
1913
1914 b_enc->noconv = true;
1915 b_enc->use_disp = false;
1916
1917 if (!(flags & SEC_ENCRYPT))
1918 b_enc->encoding = b->encoding;
1919
1920cleanup:
1921 buf_pool_release(&pgpinfile);
1922 buf_pool_release(&pgpoutfile);
1923 return b_enc;
1924}
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:577
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
const char * cc_charset(void)
Get the cached value of $charset.
int mutt_any_key_to_continue(const char *s)
Prompt the user to 'press any key' and wait.
Definition curs_lib.c:174
struct Body * mutt_body_new(void)
Create a new Body.
Definition body.c:44
char * mutt_body_get_charset(struct Body *b, char *buf, size_t buflen)
Get a body's character set.
Definition body.c:134
int mutt_file_copy_stream(FILE *fp_in, FILE *fp_out)
Copy the contents of one file into another.
Definition file.c:224
void mutt_file_unlink(const char *s)
Delete a file, carefully.
Definition file.c:156
#define mutt_file_fclose(FP)
Definition file.h:144
#define mutt_file_fopen(PATH, MODE)
Definition file.h:143
void pgp_class_void_passphrase(void)
Forget the cached passphrase - Implements CryptModuleSpecs::void_passphrase() -.
Definition pgp.c:71
#define mutt_perror(...)
Definition logging2.h:95
@ ENC_7BIT
7-bit text
Definition mime.h:49
@ TYPE_TEXT
Type: 'text/*'.
Definition mime.h:38
@ DISP_NONE
No preferred disposition.
Definition mime.h:65
@ MODULE_ID_NCRYPT
ModuleNcrypt, Ncrypt
Definition module_api.h:82
int mutt_ch_fgetconv(struct FgetConv *fc)
Convert a file's character set.
Definition charset.c:968
struct FgetConv * mutt_ch_fgetconv_open(FILE *fp, const char *from, const char *to, uint8_t flags)
Prepare a file for charset conversion.
Definition charset.c:921
void mutt_ch_fgetconv_close(struct FgetConv **ptr)
Close an fgetconv handle.
Definition charset.c:950
#define MUTT_ICONV_NONE
No flags are set.
Definition charset.h:66
#define mutt_ch_is_us_ascii(str)
Definition charset.h:108
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition filter.c:231
#define _(a)
Definition message.h:28
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:678
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
@ SEC_SIGN
Email is signed.
Definition lib.h:93
@ SEC_ENCRYPT
Email is encrypted.
Definition lib.h:92
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:666
void mutt_param_set(struct ParameterList *pl, const char *attribute, const char *value)
Set a Parameter.
Definition parameter.c:111
bool pgp_use_gpg_agent(void)
Does the user want to use the gpg agent?
Definition pgp.c:124
pid_t pgp_invoke_traditional(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 *fname, const char *uids, SecurityFlags flags)
Use PGP to create in inline-signed message.
Definition pgpinvoke.c:264
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
The body of an email.
Definition body.h:36
bool noconv
Don't do character set conversion.
Definition body.h:46
bool unlink
If true, filename should be unlink()ed before free()ing this structure.
Definition body.h:68
struct ParameterList parameter
Parameters of the content-type.
Definition body.h:63
bool use_disp
Content-Disposition uses filename= ?
Definition body.h:47
unsigned int disposition
content-disposition, ContentDisposition
Definition body.h:42
char * subtype
content-type subtype
Definition body.h:61
unsigned int encoding
content-transfer-encoding, ContentEncoding
Definition body.h:41
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:59
String manipulation buffer.
Definition buffer.h:36
Cursor for converting a file's encoding.
Definition charset.h:45
Ncrypt private Module data.
Definition module_data.h:39
char pgp_pass[1024]
Cached PGP Passphrase.
Definition module_data.h:51
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
#define buf_mktemp(buf)
Definition tmp.h:33
#define mutt_file_mkstemp()
Definition tmp.h:36
Here is the call graph for this function: