+ freeaddrinfo(ai);
+}
+
+static int can_test(void)
+{
+ int fd;
+ int n;
+ char *setup = "nfsd 0.0.0.0 2147483647 -test-client-\n";
+ fd = open("/proc/net/rpc/auth.unix.ip/channel", O_WRONLY);
+ if ( fd < 0) return 0;
+ n = write(fd, setup, strlen(setup));
+ close(fd);
+ if (n < 0)
+ return 0;
+ fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
+ if ( fd < 0) return 0;
+ close(fd);
+ return 1;
+}
+
+static int test_export(char *path, int with_fsid)
+{
+ char buf[1024];
+ int fd, n;
+
+ sprintf(buf, "-test-client- %s 3 %d -1 -1 0\n",
+ path,
+ with_fsid ? NFSEXP_FSID : 0);
+ fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
+ if (fd < 0)
+ return 0;
+ n = write(fd, buf, strlen(buf));
+ close(fd);
+ if (n < 0)
+ return 0;
+ return 1;
+}
+
+static void
+validate_export(nfs_export *exp)
+{
+ /* Check that the given export point is potentially exportable.
+ * We just give warnings here, don't cause anything to fail.
+ * If a path doesn't exist, or is not a dir or file, give an warning
+ * otherwise trial-export to '-test-client-' and check for failure.
+ */
+ struct stat stb;
+ char *path = exp->m_export.e_path;
+ struct statfs64 stf;
+ int fs_has_fsid = 0;
+
+ if (stat(path, &stb) < 0) {
+ xlog(L_ERROR, "Failed to stat %s: %m \n", path);
+ return;
+ }
+ if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
+ xlog(L_ERROR, "%s is neither a directory nor a file. "
+ "Remote access will fail", path);
+ return;
+ }
+ if (!can_test())
+ return;
+
+ if (!statfs64(path, &stf) &&
+ (stf.f_fsid.__val[0] || stf.f_fsid.__val[1]))
+ fs_has_fsid = 1;
+
+ if ((exp->m_export.e_flags & NFSEXP_FSID) || exp->m_export.e_uuid ||
+ fs_has_fsid) {
+ if ( !test_export(path, 1)) {
+ xlog(L_ERROR, "%s does not support NFS export", path);
+ return;
+ }
+ } else if ( ! test_export(path, 0)) {
+ if (test_export(path, 1))
+ xlog(L_ERROR, "%s requires fsid= for NFS export", path);
+ else
+ xlog(L_ERROR, "%s does not support NFS export", path);
+ return;
+
+ }
+}
+
+static _Bool
+is_hostname(const char *sp)
+{
+ if (*sp == '\0' || *sp == '@')
+ return false;
+
+ for (; *sp != '\0'; sp++) {
+ if (*sp == '*' || *sp == '?' || *sp == '[' || *sp == '/')
+ return false;
+ if (*sp == '\\' && sp[1] != '\0')
+ sp++;
+ }
+
+ return true;
+}
+
+static int
+matchhostname(const char *hostname1, const char *hostname2)
+{
+ struct addrinfo *results1 = NULL, *results2 = NULL;
+ struct addrinfo *ai1, *ai2;
+ int result = 0;
+
+ if (strcasecmp(hostname1, hostname2) == 0)
+ return 1;
+
+ /*
+ * Don't pass export wildcards or netgroup names to DNS
+ */
+ if (!is_hostname(hostname1) || !is_hostname(hostname2))
+ return 0;
+
+ results1 = host_addrinfo(hostname1);
+ if (results1 == NULL)
+ goto out;
+ results2 = host_addrinfo(hostname2);
+ if (results2 == NULL)
+ goto out;
+
+ if (strcasecmp(results1->ai_canonname, results2->ai_canonname) == 0) {
+ result = 1;
+ goto out;
+ }
+
+ for (ai1 = results1; ai1 != NULL; ai1 = ai1->ai_next)
+ for (ai2 = results2; ai2 != NULL; ai2 = ai2->ai_next)
+ if (nfs_compare_sockaddr(ai1->ai_addr, ai2->ai_addr)) {
+ result = 1;
+ break;
+ }
+
+out:
+ freeaddrinfo(results1);
+ freeaddrinfo(results2);
+ return result;