NetBSD/lib/libperfuse/subr.c
manu c3c545a544 - Implement proper unprivilegied user permission verifications
Verification is now done in the lookup method, as it is the way to
go. Of course there are corner cases, such as the sticky bit which
need special handling in the remove method.

- Set full fsidx in vftstat method

- Do not pass O_APPEND to the filesystem. FUSE always sends the
write offset, so setting O_APPEND is useless. If the filesystem
uses it in an open(2) system call, it will even cause file
corruptions, since offsets given to pwrite(2) will be ignored.
This fix allows glusterfs to host a NetBSD ./build.sh -o build

- Do not use the FUSE access method, use getattr and check for
permission on our own. The problem is that a FUSE filesystem will
typically use the Linux-specific setfsuid() to perform access
control. If that is missing, any chack is likely to occur on
behalf of the user running the filesystem (typically root), causing
access method to return wrong information.

- When possible, avoid performing a getattr method call and use
cached value in puffs_node instead. We still retreive the latest
value by calling getattr when performing append write operation,
to minimize the chances that another writer appended since the
last time we did.

- Update puffs_node cached file size in write method

- Remove unused argument to perfuse_destroy_pn()
2011-04-25 04:54:53 +00:00

236 lines
5.5 KiB
C

/* $NetBSD: subr.c,v 1.11 2011/04/25 04:54:53 manu Exp $ */
/*-
* Copyright (c) 2010-2011 Emmanuel Dreyfus. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <errno.h>
#include <sysexits.h>
#include <syslog.h>
#include <puffs.h>
#include <paths.h>
#include "perfuse_priv.h"
static size_t node_path(puffs_cookie_t, char *, size_t);
struct puffs_node *
perfuse_new_pn(pu, name, parent)
struct puffs_usermount *pu;
const char *name;
struct puffs_node *parent;
{
struct puffs_node *pn;
struct perfuse_node_data *pnd;
if ((pnd = malloc(sizeof(*pnd))) == NULL)
DERR(EX_OSERR, "malloc failed");
if ((pn = puffs_pn_new(pu, pnd)) == NULL)
DERR(EX_SOFTWARE, "puffs_pn_new failed");
(void)memset(pnd, 0, sizeof(*pnd));
pnd->pnd_rfh = FUSE_UNKNOWN_FH;
pnd->pnd_wfh = FUSE_UNKNOWN_FH;
pnd->pnd_ino = PERFUSE_UNKNOWN_INO;
pnd->pnd_nlookup = 1;
pnd->pnd_parent = parent;
pnd->pnd_pn = (puffs_cookie_t)pn;
(void)strlcpy(pnd->pnd_name, name, MAXPATHLEN);
TAILQ_INIT(&pnd->pnd_pcq);
TAILQ_INIT(&pnd->pnd_children);
if (parent != NULL) {
struct perfuse_node_data *parent_pnd;
parent_pnd = PERFUSE_NODE_DATA(parent);
TAILQ_INSERT_TAIL(&parent_pnd->pnd_children, pnd, pnd_next);
parent_pnd->pnd_childcount++;
}
return pn;
}
void
perfuse_destroy_pn(pn)
struct puffs_node *pn;
{
struct perfuse_node_data *pnd;
pnd = PERFUSE_NODE_DATA(pn);
if (pnd->pnd_parent != NULL) {
struct perfuse_node_data *parent_pnd;
parent_pnd = PERFUSE_NODE_DATA(pnd->pnd_parent);
TAILQ_REMOVE(&parent_pnd->pnd_children, pnd, pnd_next);
}
if ((pnd = puffs_pn_getpriv(pn)) != NULL) {
if (pnd->pnd_parent != NULL)
PERFUSE_NODE_DATA(pnd->pnd_parent)->pnd_childcount--;
if (pnd->pnd_dirent != NULL)
free(pnd->pnd_dirent);
if (pnd->pnd_all_fd != NULL)
free(pnd->pnd_all_fd);
#ifdef PERFUSE_DEBUG
if (pnd->pnd_flags & PND_OPEN)
DERRX(EX_SOFTWARE, "%s: file open", __func__);
if (!TAILQ_EMPTY(&pnd->pnd_pcq))
DERRX(EX_SOFTWARE, "%s: non empty pnd_pcq", __func__);
#endif /* PERFUSE_DEBUG */
free(pnd);
}
puffs_pn_put(pn);
return;
}
void
perfuse_new_fh(opc, fh, mode)
puffs_cookie_t opc;
uint64_t fh;
int mode;
{
struct perfuse_node_data *pnd;
pnd = PERFUSE_NODE_DATA(opc);
if (mode & FWRITE) {
if (pnd->pnd_flags & PND_WFH)
DERRX(EX_SOFTWARE, "%s: opc = %p, write fh already set",
__func__, (void *)opc);
pnd->pnd_wfh = fh;
pnd->pnd_flags |= PND_WFH;
}
if (mode & FREAD) {
if (pnd->pnd_flags & PND_RFH)
DERRX(EX_SOFTWARE, "%s: opc = %p, read fh already set",
__func__, (void *)opc);
pnd->pnd_rfh = fh;
pnd->pnd_flags |= PND_RFH;
}
return;
}
void
perfuse_destroy_fh(opc, fh)
puffs_cookie_t opc;
uint64_t fh;
{
struct perfuse_node_data *pnd;
pnd = PERFUSE_NODE_DATA(opc);
if (fh == pnd->pnd_rfh) {
if (!(pnd->pnd_flags & PND_RFH) && (fh != FUSE_UNKNOWN_FH))
DERRX(EX_SOFTWARE,
"%s: opc = %p, unset rfh = %"PRIx64"",
__func__, (void *)opc, fh);
pnd->pnd_rfh = FUSE_UNKNOWN_FH;
pnd->pnd_flags &= ~PND_RFH;
}
if (fh == pnd->pnd_wfh) {
if (!(pnd->pnd_flags & PND_WFH) && (fh != FUSE_UNKNOWN_FH))
DERRX(EX_SOFTWARE,
"%s: opc = %p, unset wfh = %"PRIx64"",
__func__, (void *)opc, fh);
pnd->pnd_wfh = FUSE_UNKNOWN_FH;
pnd->pnd_flags &= ~PND_WFH;
}
return;
}
uint64_t
perfuse_get_fh(opc, mode)
puffs_cookie_t opc;
int mode;
{
struct perfuse_node_data *pnd;
pnd = PERFUSE_NODE_DATA(opc);
if (mode & FWRITE) {
if (pnd->pnd_flags & PND_WFH)
return pnd->pnd_wfh;
}
if (mode & FREAD) {
if (pnd->pnd_flags & PND_RFH)
return pnd->pnd_rfh;
if (pnd->pnd_flags & PND_WFH)
return pnd->pnd_wfh;
}
return FUSE_UNKNOWN_FH;
}
static size_t
node_path(opc, buf, buflen)
puffs_cookie_t opc;
char *buf;
size_t buflen;
{
struct perfuse_node_data *pnd;
size_t written;
pnd = PERFUSE_NODE_DATA(opc);
if (pnd->pnd_parent == opc)
return 0;
written = node_path(pnd->pnd_parent, buf, buflen);
buf += written;
buflen -= written;
return written + snprintf(buf, buflen, "/%s", pnd->pnd_name);
}
char *
perfuse_node_path(opc)
puffs_cookie_t opc;
{
static char buf[MAXPATHLEN + 1];
if (node_path(opc, buf, sizeof(buf)) == 0)
sprintf(buf, "/");
return buf;
}