1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
/**
* @file
* Wrapper around calls to external PGP program
*
* @authors
* Copyright (C) 2017-2024 Richard Russon <rich@flatcap.org>
* Copyright (C) 2017-2023 Pietro Cerutti <gahr@gahr.ch>
* Copyright (C) 2023-2024 Tóth János <gomba007@gmail.com>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page crypt_pgpinvoke Wrapper around calls to external PGP program
*
* This file contains the new pgp invocation code.
*
* @note This is almost entirely format based.
*/
#include "config.h"
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "pgpinvoke.h"
#include "lib.h"
#include "expando/lib.h"
#include "expando_command.h"
#include "mutt_logging.h"
#include "pgpkey.h"
#include "protos.h"
#ifdef CRYPT_BACKEND_CLASSIC_PGP
#include "pgp.h"
#endif
/**
* mutt_pgp_command - Prepare a PGP Command
* @param buf Buffer for the result
* @param cctx Data to pass to the formatter
* @param exp Expando to use
*/
static void mutt_pgp_command(struct Buffer *buf, struct PgpCommandContext *cctx,
const struct Expando *exp)
{
expando_render(exp, PgpCommandRenderCallbacks, cctx, MUTT_FORMAT_NO_FLAGS, buf->dsize, buf);
mutt_debug(LL_DEBUG2, "%s\n", buf_string(buf));
}
/**
* pgp_invoke - Run a PGP command
* @param[out] fp_pgp_in stdin for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_out stdout for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_err stderr for the command, or NULL (OPTIONAL)
* @param[in] fd_pgp_in stdin for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_out stdout for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_err stderr for the command, or -1 (OPTIONAL)
* @param[in] need_passphrase Is a passphrase needed?
* @param[in] fname Filename to pass to the command
* @param[in] sig_fname Signature filename to pass to the command
* @param[in] ids List of IDs/fingerprints, space separated
* @param[in] exp Expando format string
* @retval num PID of the created process
* @retval -1 Error creating pipes or forking
*
* @note `fp_pgp_in` has priority over `fd_pgp_in`.
* Likewise `fp_pgp_out` and `fp_pgp_err`.
*/
static pid_t pgp_invoke(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
int fd_pgp_in, int fd_pgp_out, int fd_pgp_err,
bool need_passphrase, const char *fname, const char *sig_fname,
const char *ids, const struct Expando *exp)
{
struct PgpCommandContext cctx = { 0 };
if (!exp)
return (pid_t) -1;
cctx.need_passphrase = need_passphrase;
cctx.fname = fname;
cctx.sig_fname = sig_fname;
const char *const c_pgp_sign_as = cs_subset_string(NeoMutt->sub, "pgp_sign_as");
const char *const c_pgp_default_key = cs_subset_string(NeoMutt->sub, "pgp_default_key");
if (c_pgp_sign_as)
cctx.signas = c_pgp_sign_as;
else
cctx.signas = c_pgp_default_key;
cctx.ids = ids;
struct Buffer *cmd = buf_pool_get();
mutt_pgp_command(cmd, &cctx, exp);
pid_t pid = filter_create_fd(buf_string(cmd), fp_pgp_in, fp_pgp_out, fp_pgp_err,
fd_pgp_in, fd_pgp_out, fd_pgp_err, NeoMutt->env);
buf_pool_release(&cmd);
return pid;
}
/**
* pgp_invoke_decode - Use PGP to decode a message
* @param[out] fp_pgp_in stdin for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_out stdout for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_err stderr for the command, or NULL (OPTIONAL)
* @param[in] fd_pgp_in stdin for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_out stdout for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_err stderr for the command, or -1 (OPTIONAL)
* @param[in] fname Filename to pass to the command
* @param[in] need_passphrase Is a passphrase needed?
* @retval num PID of the created process
* @retval -1 Error creating pipes or forking
*
* @note `fp_pgp_in` has priority over `fd_pgp_in`.
* Likewise `fp_pgp_out` and `fp_pgp_err`.
*/
pid_t pgp_invoke_decode(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, bool need_passphrase)
{
const struct Expando *c_pgp_decode_command = cs_subset_expando(NeoMutt->sub, "pgp_decode_command");
return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out, fd_pgp_err,
need_passphrase, fname, NULL, NULL, c_pgp_decode_command);
}
/**
* pgp_invoke_verify - Use PGP to verify a message
* @param[out] fp_pgp_in stdin for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_out stdout for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_err stderr for the command, or NULL (OPTIONAL)
* @param[in] fd_pgp_in stdin for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_out stdout for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_err stderr for the command, or -1 (OPTIONAL)
* @param[in] fname Filename to pass to the command
* @param[in] sig_fname Signature filename to pass to the command
* @retval num PID of the created process
* @retval -1 Error creating pipes or forking
*
* @note `fp_pgp_in` has priority over `fd_pgp_in`.
* Likewise `fp_pgp_out` and `fp_pgp_err`.
*/
pid_t pgp_invoke_verify(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 *sig_fname)
{
const struct Expando *c_pgp_verify_command = cs_subset_expando(NeoMutt->sub, "pgp_verify_command");
return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
fd_pgp_err, false, fname, sig_fname, NULL, c_pgp_verify_command);
}
/**
* pgp_invoke_decrypt - Use PGP to decrypt a file
* @param[out] fp_pgp_in stdin for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_out stdout for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_err stderr for the command, or NULL (OPTIONAL)
* @param[in] fd_pgp_in stdin for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_out stdout for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_err stderr for the command, or -1 (OPTIONAL)
* @param[in] fname Filename to pass to the command
* @retval num PID of the created process
* @retval -1 Error creating pipes or forking
*
* @note `fp_pgp_in` has priority over `fd_pgp_in`.
* Likewise `fp_pgp_out` and `fp_pgp_err`.
*/
pid_t pgp_invoke_decrypt(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 struct Expando *c_pgp_decrypt_command = cs_subset_expando(NeoMutt->sub, "pgp_decrypt_command");
return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
fd_pgp_err, true, fname, NULL, NULL, c_pgp_decrypt_command);
}
/**
* pgp_invoke_sign - Use PGP to sign a file
* @param[out] fp_pgp_in stdin for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_out stdout for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_err stderr for the command, or NULL (OPTIONAL)
* @param[in] fd_pgp_in stdin for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_out stdout for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_err stderr for the command, or -1 (OPTIONAL)
* @param[in] fname Filename to pass to the command
* @retval num PID of the created process
* @retval -1 Error creating pipes or forking
*
* @note `fp_pgp_in` has priority over `fd_pgp_in`.
* Likewise `fp_pgp_out` and `fp_pgp_err`.
*/
pid_t pgp_invoke_sign(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 struct Expando *c_pgp_sign_command = cs_subset_expando(NeoMutt->sub, "pgp_sign_command");
return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
fd_pgp_err, true, fname, NULL, NULL, c_pgp_sign_command);
}
/**
* pgp_invoke_encrypt - Use PGP to encrypt a file
* @param[out] fp_pgp_in stdin for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_out stdout for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_err stderr for the command, or NULL (OPTIONAL)
* @param[in] fd_pgp_in stdin for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_out stdout for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_err stderr for the command, or -1 (OPTIONAL)
* @param[in] fname Filename to pass to the command
* @param[in] uids List of IDs/fingerprints, space separated
* @param[in] sign If true, also sign the file
* @retval num PID of the created process
* @retval -1 Error creating pipes or forking
*
* @note `fp_pgp_in` has priority over `fd_pgp_in`.
* Likewise `fp_pgp_out` and `fp_pgp_err`.
*/
pid_t pgp_invoke_encrypt(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, bool sign)
{
if (sign)
{
const struct Expando *c_pgp_encrypt_sign_command = cs_subset_expando(NeoMutt->sub, "pgp_encrypt_sign_command");
return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
fd_pgp_err, true, fname, NULL, uids, c_pgp_encrypt_sign_command);
}
else
{
const struct Expando *c_pgp_encrypt_only_command = cs_subset_expando(NeoMutt->sub, "pgp_encrypt_only_command");
return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
fd_pgp_err, false, fname, NULL, uids, c_pgp_encrypt_only_command);
}
}
/**
* pgp_invoke_traditional - Use PGP to create in inline-signed message
* @param[out] fp_pgp_in stdin for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_out stdout for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_err stderr for the command, or NULL (OPTIONAL)
* @param[in] fd_pgp_in stdin for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_out stdout for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_err stderr for the command, or -1 (OPTIONAL)
* @param[in] fname Filename to pass to the command
* @param[in] uids List of IDs/fingerprints, space separated
* @param[in] flags Flags, see #SecurityFlags
* @retval num PID of the created process
* @retval -1 Error creating pipes or forking
*
* @note `fp_pgp_in` has priority over `fd_pgp_in`.
* Likewise `fp_pgp_out` and `fp_pgp_err`.
*/
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)
{
if (flags & SEC_ENCRYPT)
{
const struct Expando *c_pgp_encrypt_only_command = cs_subset_expando(NeoMutt->sub, "pgp_encrypt_only_command");
const struct Expando *c_pgp_encrypt_sign_command = cs_subset_expando(NeoMutt->sub, "pgp_encrypt_sign_command");
return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
fd_pgp_err, (flags & SEC_SIGN), fname, NULL, uids,
(flags & SEC_SIGN) ? c_pgp_encrypt_sign_command : c_pgp_encrypt_only_command);
}
else
{
const struct Expando *c_pgp_clear_sign_command = cs_subset_expando(NeoMutt->sub, "pgp_clear_sign_command");
return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
fd_pgp_err, true, fname, NULL, NULL, c_pgp_clear_sign_command);
}
}
/**
* pgp_class_invoke_import - Import a key from a message into the user's public key ring - Implements CryptModuleSpecs::pgp_invoke_import() - @ingroup crypto_pgp_invoke_import
*/
void pgp_class_invoke_import(const char *fname)
{
struct PgpCommandContext cctx = { 0 };
struct Buffer *buf_fname = buf_pool_get();
struct Buffer *cmd = buf_pool_get();
buf_quote_filename(buf_fname, fname, true);
cctx.fname = buf_string(buf_fname);
const char *const c_pgp_sign_as = cs_subset_string(NeoMutt->sub, "pgp_sign_as");
const char *const c_pgp_default_key = cs_subset_string(NeoMutt->sub, "pgp_default_key");
if (c_pgp_sign_as)
cctx.signas = c_pgp_sign_as;
else
cctx.signas = c_pgp_default_key;
const struct Expando *c_pgp_import_command = cs_subset_expando(NeoMutt->sub, "pgp_import_command");
mutt_pgp_command(cmd, &cctx, c_pgp_import_command);
if (mutt_system(buf_string(cmd)) != 0)
mutt_debug(LL_DEBUG1, "Error running \"%s\"\n", buf_string(cmd));
buf_pool_release(&buf_fname);
buf_pool_release(&cmd);
}
/**
* pgp_class_invoke_getkeys - Run a command to download a PGP key - Implements CryptModuleSpecs::pgp_invoke_getkeys() - @ingroup crypto_pgp_invoke_getkeys
*/
void pgp_class_invoke_getkeys(struct Address *addr)
{
struct Buffer *personal = NULL;
struct PgpCommandContext cctx = { 0 };
const struct Expando *c_pgp_get_keys_command = cs_subset_expando(NeoMutt->sub, "pgp_get_keys_command");
if (!c_pgp_get_keys_command)
return;
struct Buffer *buf = buf_pool_get();
struct Buffer *cmd = buf_pool_get();
personal = addr->personal;
addr->personal = NULL;
struct Buffer *tmp = buf_pool_get();
mutt_addr_to_local(addr);
mutt_addr_write(tmp, addr, false);
buf_quote_filename(buf, buf_string(tmp), true);
buf_pool_release(&tmp);
addr->personal = personal;
cctx.ids = buf_string(buf);
mutt_pgp_command(cmd, &cctx, c_pgp_get_keys_command);
int fd_null = open("/dev/null", O_RDWR);
if (!isendwin())
mutt_message(_("Fetching PGP key..."));
if (mutt_system(buf_string(cmd)) != 0)
mutt_debug(LL_DEBUG1, "Error running \"%s\"\n", buf_string(cmd));
if (!isendwin())
mutt_clear_error();
if (fd_null >= 0)
close(fd_null);
buf_pool_release(&buf);
buf_pool_release(&cmd);
}
/**
* pgp_invoke_export - Use PGP to export a key from the user's keyring
* @param[out] fp_pgp_in stdin for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_out stdout for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_err stderr for the command, or NULL (OPTIONAL)
* @param[in] fd_pgp_in stdin for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_out stdout for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_err stderr for the command, or -1 (OPTIONAL)
* @param[in] uids List of IDs/fingerprints, space separated
* @retval num PID of the created process
* @retval -1 Error creating pipes or forking
*
* @note `fp_pgp_in` has priority over `fd_pgp_in`.
* Likewise `fp_pgp_out` and `fp_pgp_err`.
*/
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)
{
const struct Expando *c_pgp_export_command = cs_subset_expando(NeoMutt->sub, "pgp_export_command");
return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
fd_pgp_err, false, NULL, NULL, uids, c_pgp_export_command);
}
/**
* pgp_invoke_verify_key - Use PGP to verify a key
* @param[out] fp_pgp_in stdin for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_out stdout for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_err stderr for the command, or NULL (OPTIONAL)
* @param[in] fd_pgp_in stdin for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_out stdout for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_err stderr for the command, or -1 (OPTIONAL)
* @param[in] uids List of IDs/fingerprints, space separated
* @retval num PID of the created process
* @retval -1 Error creating pipes or forking
*
* @note `fp_pgp_in` has priority over `fd_pgp_in`.
* Likewise `fp_pgp_out` and `fp_pgp_err`.
*/
pid_t pgp_invoke_verify_key(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)
{
const struct Expando *c_pgp_verify_key_command = cs_subset_expando(NeoMutt->sub, "pgp_verify_key_command");
return pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in, fd_pgp_out,
fd_pgp_err, false, NULL, NULL, uids, c_pgp_verify_key_command);
}
/**
* pgp_invoke_list_keys - Find matching PGP Keys
* @param[out] fp_pgp_in stdin for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_out stdout for the command, or NULL (OPTIONAL)
* @param[out] fp_pgp_err stderr for the command, or NULL (OPTIONAL)
* @param[in] fd_pgp_in stdin for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_out stdout for the command, or -1 (OPTIONAL)
* @param[in] fd_pgp_err stderr for the command, or -1 (OPTIONAL)
* @param[in] keyring Keyring type, e.g. #PGP_SECRING
* @param[in] hints Match keys to these strings
* @retval num PID of the created process
* @retval -1 Error creating pipes or forking
*
* @note `fp_pgp_in` has priority over `fd_pgp_in`.
* Likewise `fp_pgp_out` and `fp_pgp_err`.
*/
pid_t pgp_invoke_list_keys(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err,
int fd_pgp_in, int fd_pgp_out, int fd_pgp_err,
enum PgpRing keyring, struct ListHead *hints)
{
struct Buffer *uids = buf_pool_get();
struct Buffer *quoted = buf_pool_get();
struct ListNode *np = NULL;
STAILQ_FOREACH(np, hints, entries)
{
buf_quote_filename(quoted, (char *) np->data, true);
buf_addstr(uids, buf_string(quoted));
if (STAILQ_NEXT(np, entries))
buf_addch(uids, ' ');
}
const struct Expando *c_pgp_list_pubring_command = cs_subset_expando(NeoMutt->sub, "pgp_list_pubring_command");
const struct Expando *c_pgp_list_secring_command = cs_subset_expando(NeoMutt->sub, "pgp_list_secring_command");
pid_t rc = pgp_invoke(fp_pgp_in, fp_pgp_out, fp_pgp_err, fd_pgp_in,
fd_pgp_out, fd_pgp_err, 0, NULL, NULL, buf_string(uids),
(keyring == PGP_SECRING) ? c_pgp_list_secring_command :
c_pgp_list_pubring_command);
buf_pool_release(&uids);
buf_pool_release("ed);
return rc;
}
|