@@ -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
@@ -10,6 +10,7 @@ noinst_HEADERS = \
misc.h \
nfs_mntent.h \
nfs_paths.h \
+ nfsd_path.h \
nfslib.h \
nfsrpc.h \
nls.h \
new file mode 100644
@@ -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
@@ -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
new file mode 100644
@@ -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
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