This patch remove the static set_path() function which was used only twice.
in:
- mutt_open_read_compressed()
We just create a temporary file and open it, so this is more secure
against symlink attacks and then just call decompression cmd normaly
- mutt_open_append_compressed()
Instead of creating the file if (cond), unlink() it if (!cond).
Signed-off-by: Julien Danjou <julien@danjou.info>
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
}
static void mbox_unlock_compressed (CONTEXT * ctx, FILE * fp)
}
static void mbox_unlock_compressed (CONTEXT * ctx, FILE * fp)
static int is_new (const char *path)
{
static int is_new (const char *path)
{
- return (access (path, W_OK) != 0 && errno == ENOENT) ? 1 : 0;
+ return (access (path, W_OK) && errno == ENOENT);
}
static const char *find_compress_hook (int type, const char *path)
}
static const char *find_compress_hook (int type, const char *path)
return (ctx->cinfo = ci);
}
return (ctx->cinfo = ci);
}
-static void set_path (CONTEXT * ctx)
-{
- char tmppath[_POSIX_PATH_MAX];
-
- /* Setup the right paths */
- ctx->realpath = ctx->path;
-
- /* Uncompress to /tmp */
- mutt_mktemp (tmppath);
- ctx->path = p_dupstr(tmppath, m_strlen(tmppath));
-}
-
static int get_size (const char *path)
{
struct stat sb;
if (stat (path, &sb) != 0)
return 0;
static int get_size (const char *path)
{
struct stat sb;
if (stat (path, &sb) != 0)
return 0;
snprintf (dest, destlen, tmp, ctx->path);
break;
}
snprintf (dest, destlen, tmp, ctx->path);
break;
}
}
/* check that the command has both %f and %t
}
/* check that the command has both %f and %t
p_delete(&ctx->realpath);
mutt_error _("Mailbox was corrupted!");
p_delete(&ctx->realpath);
mutt_error _("Mailbox was corrupted!");
}
int mutt_open_read_compressed (CONTEXT * ctx)
}
int mutt_open_read_compressed (CONTEXT * ctx)
char *cmd;
FILE *fp;
int rc;
char *cmd;
FILE *fp;
int rc;
+ char tmppath[_POSIX_PATH_MAX];
+ int tmpfd;
compress_info *ci = set_compress_info (ctx);
if (!ci->open) {
ctx->magic = 0;
p_delete(&ctx->cinfo);
compress_info *ci = set_compress_info (ctx);
if (!ci->open) {
ctx->magic = 0;
p_delete(&ctx->cinfo);
}
if (!ci->close || access (ctx->path, W_OK) != 0)
ctx->readonly = 1;
}
if (!ci->close || access (ctx->path, W_OK) != 0)
ctx->readonly = 1;
+ /* Setup the right paths */
+ ctx->realpath = ctx->path;
+
+ /* Uncompress to /tmp */
+ tmpfd = m_tempfd(tmppath, sizeof(tmppath), NONULL(Tempdir), NULL);
+ /* If we cannot open tempfile, that means the file already exists (!?)
+ * or we are following a symlink, which is bad and insecure.
+ */
+ if(!tmpfd) {
+ return -1;
+ }
+ close(tmpfd);
+
+ ctx->path = p_dupstr(tmppath, m_strlen(tmppath));
+
ctx->cinfo->size = get_size(ctx->realpath);
if (!ctx->quiet)
ctx->cinfo->size = get_size(ctx->realpath);
if (!ctx->quiet)
cmd = get_compression_cmd (ci->open, ctx);
if (cmd == NULL)
cmd = get_compression_cmd (ci->open, ctx);
if (cmd == NULL)
if ((fp = fopen (ctx->realpath, "r")) == NULL) {
mutt_perror (ctx->realpath);
p_delete(&cmd);
if ((fp = fopen (ctx->realpath, "r")) == NULL) {
mutt_perror (ctx->realpath);
p_delete(&cmd);
}
mutt_block_signals ();
if (mbox_lock_compressed (ctx, fp, 0, 1) == -1) {
}
mutt_block_signals ();
if (mbox_lock_compressed (ctx, fp, 0, 1) == -1) {
mutt_error _("Unable to lock mailbox!");
p_delete(&cmd);
mutt_error _("Unable to lock mailbox!");
p_delete(&cmd);
}
p_delete(&cmd);
if (rc)
}
p_delete(&cmd);
if (rc)
if (mutt_check_mailbox_compressed (ctx))
if (mutt_check_mailbox_compressed (ctx))
ctx->magic = mx_get_magic (ctx->path);
ctx->magic = mx_get_magic (ctx->path);
}
static void restore_path (CONTEXT * ctx)
}
static void restore_path (CONTEXT * ctx)
{
FILE *fh;
compress_info *ci = set_compress_info (ctx);
{
FILE *fh;
compress_info *ci = set_compress_info (ctx);
+ char tmppath[_POSIX_PATH_MAX];
if (!get_append_command (ctx->path, ctx)) {
if (ci->open && ci->close)
if (!get_append_command (ctx->path, ctx)) {
if (ci->open && ci->close)
- return (mutt_open_read_compressed (ctx));
+ return mutt_open_read_compressed (ctx);
ctx->magic = 0;
p_delete(&ctx->cinfo);
ctx->magic = 0;
p_delete(&ctx->cinfo);
+ /* Setup the right paths */
+ ctx->realpath = ctx->path;
+
+ /* Uncompress to /tmp */
+ fh = m_tempfile(tmppath, sizeof(tmppath), NONULL(Tempdir), NULL);
+ m_fclose(&fh);
+
+ ctx->path = p_dupstr(tmppath, m_strlen(tmppath));
ctx->magic = DefaultMagic;
ctx->magic = DefaultMagic;
- if (!is_new (ctx->realpath))
- if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
- if ((fh = safe_fopen (ctx->path, "w")))
- m_fclose(&fh);
+ if (is_new (ctx->realpath) ||
+ (ctx->magic != M_MBOX &&
+ ctx->magic != M_MMDF))
+ unlink(tmppath);
+
/* No error checking - the parent function will catch it */
/* No error checking - the parent function will catch it */
}
/* close a compressed mailbox */
}
/* close a compressed mailbox */
cmd = get_compression_cmd (ctx->cinfo->close, ctx);
if (cmd == NULL)
cmd = get_compression_cmd (ctx->cinfo->close, ctx);
if (cmd == NULL)
if ((fp = fopen (ctx->realpath, "a")) == NULL) {
mutt_perror (ctx->realpath);
p_delete(&cmd);
if ((fp = fopen (ctx->realpath, "a")) == NULL) {
mutt_perror (ctx->realpath);
p_delete(&cmd);
}
mutt_block_signals ();
if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
}
mutt_block_signals ();
if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
ctx->cinfo->size = get_size(ctx->realpath);
p_delete(&cmd);
ctx->cinfo->size = get_size(ctx->realpath);
p_delete(&cmd);
ctx->cinfo->size = get_size(ctx->realpath);
ctx->cinfo->size = get_size(ctx->realpath);
}
int mutt_slow_close_compressed (CONTEXT * ctx)
}
int mutt_slow_close_compressed (CONTEXT * ctx)
/* if we can not or should not append, we only have to remove the
compressed info, because sync was already called */
mutt_fast_close_compressed (ctx);
/* if we can not or should not append, we only have to remove the
compressed info, because sync was already called */
mutt_fast_close_compressed (ctx);
cmd = get_compression_cmd (append, ctx);
if (cmd == NULL)
cmd = get_compression_cmd (append, ctx);
if (cmd == NULL)
if ((fp = fopen (ctx->realpath, "a")) == NULL) {
mutt_perror (ctx->realpath);
p_delete(&cmd);
if ((fp = fopen (ctx->realpath, "a")) == NULL) {
mutt_perror (ctx->realpath);
p_delete(&cmd);
}
mutt_block_signals ();
if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
}
mutt_block_signals ();
if (mbox_lock_compressed (ctx, fp, 1, 1) == -1) {
mutt_error _("Unable to lock mailbox!");
p_delete(&cmd);
mutt_error _("Unable to lock mailbox!");
p_delete(&cmd);
mbox_unlock_compressed (ctx, fp);
mutt_unblock_signals ();
m_fclose(&fp);
mbox_unlock_compressed (ctx, fp);
mutt_unblock_signals ();
m_fclose(&fp);
}
mbox_unlock_compressed (ctx, fp);
}
mbox_unlock_compressed (ctx, fp);
p_delete(&cmd);
p_delete(&ctx->cinfo);
p_delete(&cmd);
p_delete(&ctx->cinfo);
}
mx_t const compress_mx = {
}
mx_t const compress_mx = {