From 4bbedd9fae360bd3ed2a59e776b5c60f6ce0cdec Mon Sep 17 00:00:00 2001 From: Kevin McCarthy Date: Tue, 9 Apr 2019 14:04:36 -0700 Subject: [PATCH] Add mutt_buffer_quote_filename() Convert almost all the callers to use the new function. alias.c usage is a bit involved, so leave that for now. Remove unneeded index while converting from mutt_quote_filename(). Co-authored-by: Richard Russon --- mutt/file.c | 30 ++++++++++++++++++++++++++++++ mutt/file.h | 2 ++ ncrypt/pgpinvoke.c | 25 +++++++++++++++---------- ncrypt/smime.c | 23 ++++++++++++++--------- remailer.c | 9 +++++---- rfc1524.c | 26 ++++++++++++++++---------- 6 files changed, 82 insertions(+), 33 deletions(-) diff --git a/mutt/file.c b/mutt/file.c index 751c1567d..ae8cae3f9 100644 --- a/mutt/file.c +++ b/mutt/file.c @@ -793,6 +793,36 @@ size_t mutt_file_quote_filename(const char *filename, char *buf, size_t buflen) return j; } +/** + * mutt_buffer_quote_filename - Quote a filename to survive the shell's quoting rules + * @param buf Buffer for the result + * @param filename String to convert + * @retval num Bytes written to the buffer + */ +void mutt_buffer_quote_filename(struct Buffer *buf, const char *filename) +{ + if (!buf || !filename) + return; + + mutt_buffer_reset(buf); + mutt_buffer_addch(buf, '\''); + + for (; *filename != '\0'; filename++) + { + if ((*filename == '\'') || (*filename == '`')) + { + mutt_buffer_addch(buf, '\''); + mutt_buffer_addch(buf, '\\'); + mutt_buffer_addch(buf, *filename); + mutt_buffer_addch(buf, '\''); + } + else + mutt_buffer_addch(buf, *filename); + } + + mutt_buffer_addch(buf, '\''); +} + /** * mutt_file_mkdir - Recursively create directories * @param path Directories to create diff --git a/mutt/file.h b/mutt/file.h index 2515fbdcf..8928d43fa 100644 --- a/mutt/file.h +++ b/mutt/file.h @@ -121,4 +121,6 @@ void mutt_file_unlink(const char *s); void mutt_file_unlink_empty(const char *path); int mutt_file_unlock(int fd); +void mutt_buffer_quote_filename(struct Buffer *buf, const char *filename); + #endif /* MUTT_LIB_FILE_H */ diff --git a/ncrypt/pgpinvoke.c b/ncrypt/pgpinvoke.c index 2bc0fb6b2..cc5b097b0 100644 --- a/ncrypt/pgpinvoke.c +++ b/ncrypt/pgpinvoke.c @@ -404,12 +404,13 @@ pid_t pgp_invoke_traditional(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_ */ void pgp_class_invoke_import(const char *fname) { - char tmp_fname[PATH_MAX + 128]; char cmd[STR_COMMAND]; struct PgpCommandContext cctx = { 0 }; - mutt_file_quote_filename(fname, tmp_fname, sizeof(tmp_fname)); - cctx.fname = tmp_fname; + struct Buffer *buf_fname = mutt_buffer_pool_get(); + + mutt_buffer_quote_filename(buf_fname, fname); + cctx.fname = mutt_b2s(buf_fname); if (C_PgpSignAs && *C_PgpSignAs) cctx.signas = C_PgpSignAs; else @@ -418,6 +419,8 @@ void pgp_class_invoke_import(const char *fname) mutt_pgp_command(cmd, sizeof(cmd), &cctx, C_PgpImportCommand); if (mutt_system(cmd) != 0) mutt_debug(LL_DEBUG1, "Error running \"%s\"", cmd); + + mutt_buffer_pool_release(&buf_fname); } /** @@ -425,7 +428,6 @@ void pgp_class_invoke_import(const char *fname) */ void pgp_class_invoke_getkeys(struct Address *addr) { - char buf[PATH_MAX]; char tmp[1024]; char cmd[STR_COMMAND]; @@ -436,17 +438,18 @@ void pgp_class_invoke_getkeys(struct Address *addr) if (!C_PgpGetkeysCommand) return; + struct Buffer *buf = mutt_buffer_pool_get(); personal = addr->personal; addr->personal = NULL; *tmp = '\0'; mutt_addrlist_to_local(addr); mutt_addr_write_single(tmp, sizeof(tmp), addr, false); - mutt_file_quote_filename(tmp, buf, sizeof(buf)); + mutt_buffer_quote_filename(buf, tmp); addr->personal = personal; - cctx.ids = buf; + cctx.ids = mutt_b2s(buf); mutt_pgp_command(cmd, sizeof(cmd), &cctx, C_PgpGetkeysCommand); @@ -463,6 +466,8 @@ void pgp_class_invoke_getkeys(struct Address *addr) if (fd_null >= 0) close(fd_null); + + mutt_buffer_pool_release(&buf); } /** @@ -529,15 +534,14 @@ pid_t pgp_invoke_list_keys(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_er int pgpinfd, int pgpoutfd, int pgperrfd, enum PgpRing keyring, struct ListHead *hints) { - char quoted[STR_COMMAND]; - struct Buffer *uids = mutt_buffer_pool_get(); + struct Buffer *quoted = mutt_buffer_pool_get(); struct ListNode *np = NULL; STAILQ_FOREACH(np, hints, entries) { - mutt_file_quote_filename((char *) np->data, quoted, sizeof(quoted)); - mutt_buffer_addstr(uids, quoted); + mutt_buffer_quote_filename(quoted, (char *) np->data); + mutt_buffer_addstr(uids, mutt_b2s(quoted)); if (STAILQ_NEXT(np, entries)) mutt_buffer_addch(uids, ' '); } @@ -548,5 +552,6 @@ pid_t pgp_invoke_list_keys(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_er C_PgpListPubringCommand); mutt_buffer_pool_release(&uids); + mutt_buffer_pool_release("ed); return rc; } diff --git a/ncrypt/smime.c b/ncrypt/smime.c index aff6bd4b4..477627a85 100644 --- a/ncrypt/smime.c +++ b/ncrypt/smime.c @@ -225,21 +225,26 @@ static const char *fmt_smime_command(char *buf, size_t buflen, size_t col, int c { if (!optional) { - char path[PATH_MAX]; - char buf1[1024], buf2[1024]; + struct Buffer *path = mutt_buffer_pool_get(); + struct Buffer *buf1 = mutt_buffer_pool_get(); + struct Buffer *buf2 = mutt_buffer_pool_get(); struct stat sb; - mutt_str_strfcpy(path, C_SmimeCaLocation, sizeof(path)); - mutt_expand_path(path, sizeof(path)); - mutt_file_quote_filename(path, buf1, sizeof(buf1)); + mutt_buffer_strcpy(path, C_SmimeCaLocation); + mutt_buffer_expand_path(path); + mutt_buffer_quote_filename(buf1, mutt_b2s(path)); - if ((stat(path, &sb) != 0) || !S_ISDIR(sb.st_mode)) - snprintf(buf2, sizeof(buf2), "-CAfile %s", buf1); + if ((stat(mutt_b2s(path), &sb) != 0) || !S_ISDIR(sb.st_mode)) + mutt_buffer_printf(buf2, "-CAfile %s", mutt_b2s(buf1)); else - snprintf(buf2, sizeof(buf2), "-CApath %s", buf1); + mutt_buffer_printf(buf2, "-CApath %s", mutt_b2s(buf1)); snprintf(fmt, sizeof(fmt), "%%%ss", prec); - snprintf(buf, buflen, fmt, buf2); + snprintf(buf, buflen, fmt, mutt_b2s(buf2)); + + mutt_buffer_pool_release(&path); + mutt_buffer_pool_release(&buf1); + mutt_buffer_pool_release(&buf2); } else if (!C_SmimeCaLocation) optional = 0; diff --git a/remailer.c b/remailer.c index 09395f243..32807ea27 100644 --- a/remailer.c +++ b/remailer.c @@ -820,18 +820,18 @@ int mix_check_message(struct Email *msg) */ int mix_send_message(struct ListHead *chain, const char *tempfile) { - char cd_quoted[256]; int i = 0; - struct Buffer *cmd = mutt_buffer_pool_get(); + struct Buffer *cd_quoted = mutt_buffer_pool_get(); + mutt_buffer_printf(cmd, "cat %s | %s -m ", tempfile, C_Mixmaster); struct ListNode *np = NULL; STAILQ_FOREACH(np, chain, entries) { mutt_buffer_addstr(cmd, i ? "," : " -l "); - mutt_file_quote_filename(np->data, cd_quoted, sizeof(cd_quoted)); - mutt_buffer_addstr(cmd, cd_quoted); + mutt_buffer_quote_filename(cd_quoted, (char *) np->data); + mutt_buffer_addstr(cmd, mutt_b2s(cd_quoted)); i = 1; } @@ -849,6 +849,7 @@ int mix_send_message(struct ListHead *chain, const char *tempfile) } mutt_buffer_pool_release(&cmd); + mutt_buffer_pool_release(&cd_quoted); unlink(tempfile); return i; } diff --git a/rfc1524.c b/rfc1524.c index 5d7b19b75..a9376c266 100644 --- a/rfc1524.c +++ b/rfc1524.c @@ -72,22 +72,23 @@ bool C_MailcapSanitize; ///< Config: Restrict the possible characters in mailcap int rfc1524_expand_command(struct Body *a, const char *filename, const char *type, char *command, int clen) { - int x = 0, y = 0; + int x = 0; int needspipe = true; - char buf[STR_COMMAND]; char type2[1024]; + struct Buffer *buf = mutt_buffer_pool_get(); + struct Buffer *quoted = mutt_buffer_pool_get(); mutt_str_strfcpy(type2, type, sizeof(type2)); if (C_MailcapSanitize) mutt_file_sanitize_filename(type2, false); - while ((x < clen - 1) && command[x] && (y < sizeof(buf) - 1)) + while ((x < clen - 1) && command[x]) { if (command[x] == '\\') { x++; - buf[y++] = command[x++]; + mutt_buffer_addch(buf, command[x++]); } else if (command[x] == '%') { @@ -115,24 +116,29 @@ int rfc1524_expand_command(struct Body *a, const char *filename, if (C_MailcapSanitize) mutt_file_sanitize_filename(pvalue, false); - y += mutt_file_quote_filename(pvalue, buf + y, sizeof(buf) - y); + mutt_buffer_quote_filename(quoted, pvalue); + mutt_buffer_addstr(buf, mutt_b2s(quoted)); } else if ((command[x] == 's') && filename) { - y += mutt_file_quote_filename(filename, buf + y, sizeof(buf) - y); + mutt_buffer_quote_filename(quoted, filename); + mutt_buffer_addstr(buf, mutt_b2s(quoted)); needspipe = false; } else if (command[x] == 't') { - y += mutt_file_quote_filename(type2, buf + y, sizeof(buf) - y); + mutt_buffer_quote_filename(quoted, type); + mutt_buffer_addstr(buf, mutt_b2s(quoted)); } x++; } else - buf[y++] = command[x++]; + mutt_buffer_addch(buf, command[x++]); } - buf[y] = '\0'; - mutt_str_strfcpy(command, buf, clen); + mutt_str_strfcpy(command, mutt_b2s(buf), clen); + + mutt_buffer_pool_release(&buf); + mutt_buffer_pool_release("ed); return needspipe; } -- 2.40.0