summaryrefslogtreecommitdiffstats
path: root/usr.bin/tmux/cmd-save-buffer.c
diff options
context:
space:
mode:
authornicm <nicm@openbsd.org>2015-10-31 08:13:58 +0000
committernicm <nicm@openbsd.org>2015-10-31 08:13:58 +0000
commit3baa4a0c87c984716fa2bde8c3c85ab9031b05d8 (patch)
treed4a7ae30f3f6f078addaedda2c627e1216b270d9 /usr.bin/tmux/cmd-save-buffer.c
parentsync (diff)
downloadwireguard-openbsd-3baa4a0c87c984716fa2bde8c3c85ab9031b05d8.tar.xz
wireguard-openbsd-3baa4a0c87c984716fa2bde8c3c85ab9031b05d8.zip
Because pledge(2) does not allow us to pass directory file descriptors
around, we can't use file descriptors for the working directory because we will be unable to pass it to a privileged process to tell it where to read or write files or spawn children. So move tmux back to using strings for the current working directory. We try to check it exists with access() when it is set but ultimately fall back to ~ if it fails at time of use (or / if that fails too).
Diffstat (limited to 'usr.bin/tmux/cmd-save-buffer.c')
-rw-r--r--usr.bin/tmux/cmd-save-buffer.c37
1 files changed, 18 insertions, 19 deletions
diff --git a/usr.bin/tmux/cmd-save-buffer.c b/usr.bin/tmux/cmd-save-buffer.c
index 4dc46a65285..b85a16bdb63 100644
--- a/usr.bin/tmux/cmd-save-buffer.c
+++ b/usr.bin/tmux/cmd-save-buffer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cmd-save-buffer.c,v 1.30 2015/08/29 09:36:46 nicm Exp $ */
+/* $OpenBSD: cmd-save-buffer.c,v 1.31 2015/10/31 08:13:58 nicm Exp $ */
/*
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
@@ -57,10 +57,10 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
struct client *c = cmdq->client;
struct session *s;
struct paste_buffer *pb;
- const char *path, *bufname, *bufdata, *start, *end;
- char *msg;
+ const char *path, *bufname, *bufdata, *start, *end, *cwd;
+ const char *flags;
+ char *msg, *file, resolved[PATH_MAX];
size_t size, used, msglen, bufsize;
- int cwd, fd;
FILE *f;
if (!args_has(args, 'b')) {
@@ -97,26 +97,25 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
else if ((s = cmd_find_current(cmdq)) != NULL)
cwd = s->cwd;
else
- cwd = AT_FDCWD;
+ cwd = ".";
- f = NULL;
- if (args_has(self->args, 'a')) {
- fd = openat(cwd, path, O_CREAT|O_RDWR|O_APPEND, 0600);
- if (fd != -1)
- f = fdopen(fd, "ab");
- } else {
- fd = openat(cwd, path, O_CREAT|O_RDWR|O_TRUNC, 0600);
- if (fd != -1)
- f = fdopen(fd, "wb");
- }
+ flags = "wb";
+ if (args_has(self->args, 'a'))
+ flags = "ab";
+
+ xasprintf(&file, "%s/%s", cwd, path);
+ if (realpath(file, resolved) == NULL)
+ f = NULL;
+ else
+ f = fopen(resolved, flags);
+ free(file);
if (f == NULL) {
- if (fd != -1)
- close(fd);
- cmdq_error(cmdq, "%s: %s", path, strerror(errno));
+ cmdq_error(cmdq, "%s: %s", resolved, strerror(errno));
return (CMD_RETURN_ERROR);
}
+
if (fwrite(bufdata, 1, bufsize, f) != bufsize) {
- cmdq_error(cmdq, "%s: fwrite error", path);
+ cmdq_error(cmdq, "%s: write error", resolved);
fclose(f);
return (CMD_RETURN_ERROR);
}