linux-nfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Trond Myklebust <trondmy@gmail.com>
To: SteveD@redhat.com
Cc: linux-nfs@vger.kernel.org
Subject: [RFC PATCH v2 3/7] Add utilities for resolving nfsd paths and stat()ing them
Date: Tue, 21 May 2019 08:46:57 -0400	[thread overview]
Message-ID: <20190521124701.61849-4-trond.myklebust@hammerspace.com> (raw)
In-Reply-To: <20190521124701.61849-3-trond.myklebust@hammerspace.com>

Add helper functions that can resolve nfsd paths by prepending the
necessary prefix if the admin has specified a root path in the
nfs.conf file.

Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com>
---
 configure.ac                |   2 +-
 support/include/Makefile.am |   1 +
 support/include/nfsd_path.h |  17 ++++
 support/misc/Makefile.am    |   3 +-
 support/misc/nfsd_path.c    | 173 ++++++++++++++++++++++++++++++++++++
 5 files changed, 194 insertions(+), 2 deletions(-)
 create mode 100644 support/include/nfsd_path.h
 create mode 100644 support/misc/nfsd_path.c

diff --git a/configure.ac b/configure.ac
index c6c2d73b06dd..4793daeb2716 100644
--- a/configure.ac
+++ b/configure.ac
@@ -321,7 +321,7 @@ AC_CHECK_FUNC([getservbyname], ,
 AC_CHECK_LIB([crypt], [crypt], [LIBCRYPT="-lcrypt"])
 
 AC_CHECK_HEADERS([sched.h], [], [])
-AC_CHECK_FUNCS([unshare], [] , [])
+AC_CHECK_FUNCS([unshare openat fstatat], [] , [])
 AC_LIBPTHREAD([])
 
 if test "$enable_nfsv4" = yes; then
diff --git a/support/include/Makefile.am b/support/include/Makefile.am
index df5e47836d29..fbf487eee0e2 100644
--- a/support/include/Makefile.am
+++ b/support/include/Makefile.am
@@ -10,6 +10,7 @@ noinst_HEADERS = \
 	misc.h \
 	nfs_mntent.h \
 	nfs_paths.h \
+	nfsd_path.h \
 	nfslib.h \
 	nfsrpc.h \
 	nls.h \
diff --git a/support/include/nfsd_path.h b/support/include/nfsd_path.h
new file mode 100644
index 000000000000..5936cd5ed666
--- /dev/null
+++ b/support/include/nfsd_path.h
@@ -0,0 +1,17 @@
+/*
+ * Copyright (C) 2019 Trond Myklebust <trond.myklebust@hammerspace.com>
+ */
+#ifndef XPATH_H
+#define XPATH_H
+
+void 		nfsd_path_init(void);
+void 		nfsd_path_nfsd_rootfs_close(void);
+
+const char *	nfsd_path_nfsd_rootdir(void);
+char *		nfsd_path_strip_root(char *pathname);
+char *		nfsd_path_prepend_dir(const char *dir, const char *pathname);
+
+int 		nfsd_path_stat(const char *pathname, struct stat *statbuf);
+int 		nfsd_path_lstat(const char *pathname, struct stat *statbuf);
+
+#endif
diff --git a/support/misc/Makefile.am b/support/misc/Makefile.am
index d0bff8feb6ae..ff1e8ab79ae3 100644
--- a/support/misc/Makefile.am
+++ b/support/misc/Makefile.am
@@ -1,6 +1,7 @@
 ## Process this file with automake to produce Makefile.in
 
 noinst_LIBRARIES = libmisc.a
-libmisc_a_SOURCES = tcpwrapper.c from_local.c mountpoint.c file.c workqueue.c
+libmisc_a_SOURCES = tcpwrapper.c from_local.c mountpoint.c file.c \
+		    nfsd_path.c workqueue.c
 
 MAINTAINERCLEANFILES = Makefile.in
diff --git a/support/misc/nfsd_path.c b/support/misc/nfsd_path.c
new file mode 100644
index 000000000000..481ba49a38fd
--- /dev/null
+++ b/support/misc/nfsd_path.c
@@ -0,0 +1,173 @@
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "config.h"
+#include "conffile.h"
+#include "xmalloc.h"
+#include "xlog.h"
+#include "nfsd_path.h"
+
+static int
+nfsd_path_isslash(const char *path)
+{
+	return path[0] == '/' && path[1] == '/';
+}
+
+static int
+nfsd_path_isdot(const char *path)
+{
+	return path[0] == '.' && path[1] == '/';
+}
+
+static const char *
+nfsd_path_strip(const char *path)
+{
+	if (!path || *path == '\0')
+		goto out;
+	for (;;) {
+		if (nfsd_path_isslash(path)) {
+			path++;
+			continue;
+		}
+		if (nfsd_path_isdot(path)) {
+			path += 2;
+			continue;
+		}
+		break;
+	}
+out:
+	return path;
+}
+
+const char *
+nfsd_path_nfsd_rootdir(void)
+{
+	const char *rootdir;
+
+	rootdir = nfsd_path_strip(conf_get_str("nfsd", "root_dir"));
+	if (!rootdir || rootdir[0] ==  '\0')
+		return NULL;
+	if (rootdir[0] == '/' && rootdir[1] == '\0')
+		return NULL;
+	return rootdir;
+}
+
+char *
+nfsd_path_strip_root(char *pathname)
+{
+	const char *dir = nfsd_path_nfsd_rootdir();
+	char *ret;
+
+	ret = strstr(pathname, dir);
+	if (!ret || ret != pathname)
+		return pathname;
+	return pathname + strlen(dir);
+}
+
+char *
+nfsd_path_prepend_dir(const char *dir, const char *pathname)
+{
+	size_t len, dirlen;
+	char *ret;
+
+	dirlen = strlen(dir);
+	while (dirlen > 0 && dir[dirlen - 1] == '/')
+		dirlen--;
+	if (!dirlen)
+		return NULL;
+	len = dirlen + strlen(pathname) + 1;
+	ret = xmalloc(len + 1);
+	snprintf(ret, len, "%.*s/%s", (int)dirlen, dir, pathname);
+	return ret;
+}
+
+#if defined(HAVE_FSTATAT) && defined(HAVE_OPENAT)
+static int nfsd_rootfs = AT_FDCWD;
+
+void nfsd_path_nfsd_rootfs_close(void)
+{
+	if (nfsd_rootfs != AT_FDCWD) {
+		close(nfsd_rootfs);
+		nfsd_rootfs = AT_FDCWD;
+	}
+}
+
+void nfsd_path_init(void)
+{
+	const char *rootdir = nfsd_path_nfsd_rootdir();
+
+	nfsd_path_nfsd_rootfs_close();
+	if (rootdir) {
+		nfsd_rootfs = openat(AT_FDCWD, rootdir, O_PATH);
+		if (nfsd_rootfs == -1)
+			xlog_err("Could not open directory %s: %m", rootdir);
+	}
+}
+
+int nfsd_path_stat(const char *pathname, struct stat *statbuf)
+{
+	if (nfsd_rootfs != AT_FDCWD) {
+		while (pathname[0] == '/')
+			pathname++;
+	}
+	return fstatat(nfsd_rootfs, pathname, statbuf, AT_EMPTY_PATH |
+			AT_NO_AUTOMOUNT);
+}
+
+int nfsd_path_lstat(const char *pathname, struct stat *statbuf)
+{
+	if (nfsd_rootfs != AT_FDCWD) {
+		while (pathname[0] == '/')
+			pathname++;
+	}
+	return fstatat(nfsd_rootfs, pathname, statbuf, AT_EMPTY_PATH |
+			AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW);
+}
+
+#else /* defined(HAVE_FSTATAT) && defined(HAVE_OPENAT) */
+void nfsd_path_init(void)
+{
+}
+
+void nfsd_path_nfsd_rootfs_close(void)
+{
+}
+
+int nfsd_path_stat(const char *pathname, struct stat *statbuf)
+{
+	const char *rootdir = nfsd_path_nfsd_rootdir();
+	char *str;
+	int ret;
+
+	if (!rootdir)
+		goto out_stat;
+	str = nfsd_path_prepend_dir(rootdir, nfsd_path_strip(pathname));
+	if (!str)
+		goto out_stat;
+	ret = stat(str, statbuf);
+	xfree(str);
+	return ret;
+out_stat:
+	return stat(pathname, statbuf);
+}
+
+int nfsd_path_lstat(const char *pathname, struct stat *statbuf)
+{
+	const char *rootdir = nfsd_path_nfsd_rootdir();
+	char *str;
+	int ret;
+
+	if (!rootdir)
+		goto out_lstat;
+	str = nfsd_path_prepend_dir(rootdir, nfsd_path_strip(pathname));
+	if (!str)
+		goto out_lstat;
+	ret = lstat(str, statbuf);
+	xfree(str);
+	return ret;
+out_lstat:
+	return lstat(pathname, statbuf);
+}
+#endif
-- 
2.21.0


  reply	other threads:[~2019-05-21 12:49 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-21 12:46 [RFC PATCH v2 0/7] Add a root_dir option to nfs.conf Trond Myklebust
2019-05-21 12:46 ` [RFC PATCH v2 1/7] mountd: Ensure we don't share cache file descriptors among processes Trond Myklebust
2019-05-21 12:46   ` [RFC PATCH v2 2/7] Add a simple workqueue mechanism Trond Myklebust
2019-05-21 12:46     ` Trond Myklebust [this message]
2019-05-21 12:46       ` [RFC PATCH v2 4/7] Add a helper to return the real path given an export entry Trond Myklebust
2019-05-21 12:46         ` [RFC PATCH v2 5/7] Add helpers to read/write to a file through the chrooted thread Trond Myklebust
2019-05-21 12:47           ` [RFC PATCH v2 6/7] Add support for the nfsd rootdir configuration option to rpc.mountd Trond Myklebust
2019-05-21 12:47             ` [RFC PATCH v2 7/7] Add support for the nfsd root directory to exportfs Trond Myklebust
2019-05-21 17:40 ` [RFC PATCH v2 0/7] Add a root_dir option to nfs.conf Chuck Lever
2019-05-21 18:17   ` Trond Myklebust
2019-05-21 18:59     ` Trond Myklebust
2019-05-21 19:06     ` Chuck Lever
2019-05-21 19:58       ` Trond Myklebust
2019-05-28 15:25         ` Steve Dickson
2019-05-28 16:44           ` Trond Myklebust
2019-05-28 16:47             ` Chuck Lever
2019-05-28 16:50               ` Trond Myklebust
2019-05-28 17:40             ` Steve Dickson
2019-05-28 18:19               ` Trond Myklebust
2019-05-28 19:33                 ` Steve Dickson
2019-05-28 15:30         ` Chuck Lever

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190521124701.61849-4-trond.myklebust@hammerspace.com \
    --to=trondmy@gmail.com \
    --cc=SteveD@redhat.com \
    --cc=linux-nfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).