]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - support/nfs/exports.c
mountd: fix exporting of "/" with sec= setting.
[nfs-utils.git] / support / nfs / exports.c
index 963801583e57ba99adeacfd434379453ad3bad3b..dea040f1006eb830f13a031d1a55dce80555867d 100644 (file)
 #include "xmalloc.h"
 #include "xlog.h"
 #include "xio.h"
+#include "pseudoflavors.h"
 
 #define EXPORT_DEFAULT_FLAGS   \
   (NFSEXP_READONLY|NFSEXP_ROOTSQUASH|NFSEXP_GATHERED_WRITES|NFSEXP_NOSUBTREECHECK)
 
+struct flav_info flav_map[] = {
+       { "krb5",       RPC_AUTH_GSS_KRB5       },
+       { "krb5i",      RPC_AUTH_GSS_KRB5I      },
+       { "krb5p",      RPC_AUTH_GSS_KRB5P      },
+       { "unix",       AUTH_UNIX               },
+       { "sys",        AUTH_SYS                },
+       { "null",       AUTH_NULL               },
+       { "none",       AUTH_NONE               },
+};
+
+const int flav_map_size = sizeof(flav_map)/sizeof(flav_map[0]);
+
 int export_errno;
 
 static char    *efname = NULL;
@@ -48,7 +61,6 @@ static int    getpath(char *path, int len);
 static int     parseopts(char *cp, struct exportent *ep, int warn, int *had_subtree_opt_ptr);
 static int     parsesquash(char *list, int **idp, int *lenp, char **ep);
 static int     parsenum(char **cpp);
-static int     parsemaptype(char *type);
 static void    freesquash(void);
 static void    syntaxerr(char *msg);
 
@@ -66,6 +78,32 @@ setexportent(char *fname, char *type)
        first = 1;
 }
 
+static void init_exportent (struct exportent *ee, int fromkernel)
+{
+       ee->e_flags = EXPORT_DEFAULT_FLAGS;
+       /* some kernels assume the default is sync rather than
+        * async.  More recent kernels always report one or other,
+        * but this test makes sure we assume same as kernel
+        * Ditto for wgather
+        */
+       if (fromkernel) {
+               ee->e_flags &= ~NFSEXP_ASYNC;
+               ee->e_flags &= ~NFSEXP_GATHERED_WRITES;
+       }
+       ee->e_anonuid = 65534;
+       ee->e_anongid = 65534;
+       ee->e_squids = NULL;
+       ee->e_sqgids = NULL;
+       ee->e_mountpoint = NULL;
+       ee->e_fslocmethod = FSLOC_NONE;
+       ee->e_fslocdata = NULL;
+       ee->e_secinfo[0].flav = NULL;
+       ee->e_nsquids = 0;
+       ee->e_nsqgids = 0;
+       ee->e_uuid = NULL;
+       ee->e_ttl = DEFAULT_TTL;
+}
+
 struct exportent *
 getexportent(int fromkernel, int fromexports)
 {
@@ -84,33 +122,12 @@ getexportent(int fromkernel, int fromexports)
                has_default_opts = 0;
                has_default_subtree_opts = 0;
        
-               def_ee.e_flags = EXPORT_DEFAULT_FLAGS;
-               /* some kernels assume the default is sync rather than
-                * async.  More recent kernels always report one or other,
-                * but this test makes sure we assume same as kernel
-                * Ditto for wgather
-                */
-               if (fromkernel) {
-                       def_ee.e_flags &= ~NFSEXP_ASYNC;
-                       def_ee.e_flags &= ~NFSEXP_GATHERED_WRITES;
-               }
-               def_ee.e_maptype = CLE_MAP_IDENT;
-               def_ee.e_anonuid = 65534;
-               def_ee.e_anongid = 65534;
-               def_ee.e_squids = NULL;
-               def_ee.e_sqgids = NULL;
-               def_ee.e_mountpoint = NULL;
-               def_ee.e_fslocmethod = FSLOC_NONE;
-               def_ee.e_fslocdata = NULL;
-               def_ee.e_nsquids = 0;
-               def_ee.e_nsqgids = 0;
+               init_exportent(&def_ee, fromkernel);
 
                ok = getpath(def_ee.e_path, sizeof(def_ee.e_path));
                if (ok <= 0)
                        return NULL;
 
-               strncpy (def_ee.m_path, def_ee.e_path, sizeof (def_ee.m_path) - 1);
-               def_ee.m_path [sizeof (def_ee.m_path) - 1] = '\0';
                ok = getexport(exp, sizeof(exp));
        }
        if (ok < 0) {
@@ -119,9 +136,14 @@ getexportent(int fromkernel, int fromexports)
                return NULL;
        }
        first = 0;
-               
-       /* Check for default options */
-       if (exp[0] == '-') {
+
+       /*
+        * Check for default options.  The kernel will never have default
+        * options in /proc/fs/nfs/exports, however due to the initial '-' in
+        * the -test-client- string from the test export we have to check that
+        * we're not reading from the kernel.
+        */
+       if (exp[0] == '-' && !fromkernel) {
                if (parseopts(exp + 1, &def_ee, 0, &has_default_subtree_opts) < 0)
                        return NULL;
                
@@ -158,13 +180,8 @@ getexportent(int fromkernel, int fromexports)
                if (!has_default_opts)
                        xlog(L_WARNING, "No options for %s %s: suggest %s(sync) to avoid warning", ee.e_path, exp, exp);
        }
-       if (strlen(hostname) >= sizeof(ee.e_hostname)) {
-               syntaxerr("client name too long");
-               export_errno = EINVAL;
-               return NULL;
-       }
-       strncpy(ee.e_hostname, hostname, sizeof (ee.e_hostname) - 1);
-       ee.e_hostname[sizeof (ee.e_hostname) - 1] = '\0';
+       xfree(ee.e_hostname);
+       ee.e_hostname = xstrdup(hostname);
 
        if (parseopts(opt, &ee, fromexports && !has_default_subtree_opts, NULL) < 0)
                return NULL;
@@ -174,13 +191,32 @@ getexportent(int fromkernel, int fromexports)
                rpath[sizeof (rpath) - 1] = '\0';
                strncpy(ee.e_path, rpath, sizeof (ee.e_path) - 1);
                ee.e_path[sizeof (ee.e_path) - 1] = '\0';
-               strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
-               ee.m_path [sizeof (ee.m_path) - 1] = '\0';
        }
 
        return &ee;
 }
 
+void secinfo_show(FILE *fp, struct exportent *ep)
+{
+       struct sec_entry *p1, *p2;
+       int flags;
+
+       for (p1=ep->e_secinfo; p1->flav; p1=p2) {
+
+               fprintf(fp, ",sec=%s", p1->flav->flavour);
+               for (p2=p1+1; (p2->flav != NULL) && (p1->flags == p2->flags);
+                                                               p2++) {
+                       fprintf(fp, ":%s", p2->flav->flavour);
+               }
+               flags = p1->flags;
+               fprintf(fp, ",%s", (flags & NFSEXP_READONLY) ? "ro" : "rw");
+               fprintf(fp, ",%sroot_squash", (flags & NFSEXP_ROOTSQUASH)?
+                               "" : "no_");
+               fprintf(fp, ",%sall_squash", (flags & NFSEXP_ALLSQUASH)?
+                               "" : "no_");
+       }
+}
+
 void
 putexportent(struct exportent *ep)
 {
@@ -245,21 +281,6 @@ putexportent(struct exportent *ep)
                xlog(L_ERROR, "unknown fsloc method for %s:%s",
                     ep->e_hostname, ep->e_path);
        }
-       fprintf(fp, "mapping=");
-       switch (ep->e_maptype) {
-       case CLE_MAP_IDENT:
-               fprintf(fp, "identity,");
-               break;
-       case CLE_MAP_UGIDD:
-               fprintf(fp, "ugidd,");
-               break;
-       case CLE_MAP_FILE:
-               fprintf(fp, "file,");
-               break;
-       default:
-               xlog(L_ERROR, "unknown mapping type for %s:%s",
-                                       ep->e_hostname, ep->e_path);
-       }
        if ((id = ep->e_squids) != NULL) {
                fprintf(fp, "squash_uids=");
                for (i = 0; i < ep->e_nsquids; i += 2)
@@ -276,7 +297,9 @@ putexportent(struct exportent *ep)
                        else
                                fprintf(fp, "%d,", id[i]);
        }
-       fprintf(fp, "anonuid=%d,anongid=%d)\n", ep->e_anonuid, ep->e_anongid);
+       fprintf(fp, "anonuid=%d,anongid=%d", ep->e_anonuid, ep->e_anongid);
+       secinfo_show(fp, ep);
+       fprintf(fp, ")\n");
 }
 
 void
@@ -309,6 +332,9 @@ dupexportent(struct exportent *dst, struct exportent *src)
                dst->e_mountpoint = strdup(src->e_mountpoint);
        if (src->e_fslocdata)
                dst->e_fslocdata = strdup(src->e_fslocdata);
+       if (src->e_uuid)
+               dst->e_uuid = strdup(src->e_uuid);
+       dst->e_hostname = NULL;
 }
 
 struct exportent *
@@ -316,33 +342,17 @@ mkexportent(char *hname, char *path, char *options)
 {
        static struct exportent ee;
 
-       ee.e_flags = EXPORT_DEFAULT_FLAGS;
-       ee.e_maptype = CLE_MAP_IDENT;
-       ee.e_anonuid = 65534;
-       ee.e_anongid = 65534;
-       ee.e_squids = NULL;
-       ee.e_sqgids = NULL;
-       ee.e_mountpoint = NULL;
-       ee.e_fslocmethod = FSLOC_NONE;
-       ee.e_fslocdata = NULL;
-       ee.e_nsquids = 0;
-       ee.e_nsqgids = 0;
-       ee.e_uuid = NULL;
-
-       if (strlen(hname) >= sizeof(ee.e_hostname)) {
-               xlog(L_WARNING, "client name %s too long", hname);
-               return NULL;
-       }
-       strncpy(ee.e_hostname, hname, sizeof (ee.e_hostname) - 1);
-       ee.e_hostname[sizeof (ee.e_hostname) - 1] = '\0';
+       init_exportent(&ee, 0);
+
+       xfree(ee.e_hostname);
+       ee.e_hostname = xstrdup(hname);
+
        if (strlen(path) >= sizeof(ee.e_path)) {
                xlog(L_WARNING, "path name %s too long", path);
                return NULL;
        }
        strncpy(ee.e_path, path, sizeof (ee.e_path));
        ee.e_path[sizeof (ee.e_path) - 1] = '\0';
-       strncpy (ee.m_path, ee.e_path, sizeof (ee.m_path) - 1);
-       ee.m_path [sizeof (ee.m_path) - 1] = '\0';
        if (parseopts(options, &ee, 0, NULL) < 0)
                return NULL;
        return &ee;
@@ -367,6 +377,108 @@ static int valid_uuid(char *uuid)
        return cnt == 32;
 }
 
+/*
+ * Append the given flavor to the exportent's e_secinfo array, or
+ * do nothing if it's already there.  Returns the index of flavor
+ * in the resulting array in any case.
+ */
+int secinfo_addflavor(struct flav_info *flav, struct exportent *ep)
+{
+       struct sec_entry *p;
+
+       for (p=ep->e_secinfo; p->flav; p++) {
+               if (p->flav == flav)
+                       return p - ep->e_secinfo;
+       }
+       if (p - ep->e_secinfo >= SECFLAVOR_COUNT) {
+               xlog(L_ERROR, "more than %d security flavors on an export\n",
+                       SECFLAVOR_COUNT);
+               return -1;
+       }
+       p->flav = flav;
+       p->flags = ep->e_flags;
+       (p+1)->flav = NULL;
+       return p - ep->e_secinfo;
+}
+
+static struct flav_info *find_flavor(char *name)
+{
+       struct flav_info *flav;
+       for (flav = flav_map; flav < flav_map + flav_map_size; flav++)
+               if (strcmp(flav->flavour, name) == 0)
+                       return flav;
+       return NULL;
+}
+
+/* @str is a colon seperated list of security flavors.  Their order
+ * is recorded in @ep, and a bitmap corresponding to the list is returned.
+ * A zero return indicates an error.
+ */
+static unsigned int parse_flavors(char *str, struct exportent *ep)
+{
+       unsigned int out=0;
+       char *flavor;
+       int bit;
+
+       while ( (flavor=strsep(&str, ":")) ) {
+               struct flav_info *flav = find_flavor(flavor);
+               if (flav == NULL) {
+                       xlog(L_ERROR, "unknown flavor %s\n", flavor);
+                       return 0;
+               }
+               bit = secinfo_addflavor(flav, ep);
+               if (bit < 0)
+                       return 0;
+               out |= 1<<bit;
+       }
+       return out;
+}
+
+/* Sets the bits in @mask for the appropriate security flavor flags. */
+static void setflags(int mask, unsigned int active, struct exportent *ep)
+{
+       int bit=0;
+
+       ep->e_flags |= mask;
+
+       while (active) {
+               if (active & 1)
+                       ep->e_secinfo[bit].flags |= mask;
+               bit++;
+               active >>= 1;
+       }
+}
+
+/* Clears the bits in @mask for the appropriate security flavor flags. */
+static void clearflags(int mask, unsigned int active, struct exportent *ep)
+{
+       int bit=0;
+
+       ep->e_flags &= ~mask;
+
+       while (active) {
+               if (active & 1)
+                       ep->e_secinfo[bit].flags &= ~mask;
+               bit++;
+               active >>= 1;
+       }
+}
+
+/*
+ * For those flags which are not allowed to vary by pseudoflavor,
+ * ensure that the export flags agree with the flags on each
+ * pseudoflavor:
+ */
+void fix_pseudoflavor_flags(struct exportent *ep)
+{
+       struct export_features *ef;
+       struct sec_entry *p;
+
+       ef = get_export_features();
+       for (p = ep->e_secinfo; p->flav; p++)
+               p->flags |= ep->e_flags & ~ef->secinfo_flags;
+}
+
 /*
  * Parse option string pointed to by cp and set mount options accordingly.
  */
@@ -376,10 +488,10 @@ parseopts(char *cp, struct exportent *ep, int warn, int *had_subtree_opt_ptr)
        int     had_subtree_opt = 0;
        char    *flname = efname?efname:"command line";
        int     flline = efp?efp->x_line:0;
+       unsigned int active = 0;
 
        squids = ep->e_squids; nsquids = ep->e_nsquids;
        sqgids = ep->e_sqgids; nsqgids = ep->e_nsqgids;
-
        if (!cp)
                goto out;
 
@@ -398,61 +510,55 @@ parseopts(char *cp, struct exportent *ep, int warn, int *had_subtree_opt_ptr)
 
                /* process keyword */
                if (strcmp(opt, "ro") == 0)
-                       ep->e_flags |= NFSEXP_READONLY;
+                       setflags(NFSEXP_READONLY, active, ep);
                else if (strcmp(opt, "rw") == 0)
-                       ep->e_flags &= ~NFSEXP_READONLY;
+                       clearflags(NFSEXP_READONLY, active, ep);
                else if (!strcmp(opt, "secure"))
-                       ep->e_flags &= ~NFSEXP_INSECURE_PORT;
+                       clearflags(NFSEXP_INSECURE_PORT, active, ep);
                else if (!strcmp(opt, "insecure"))
-                       ep->e_flags |= NFSEXP_INSECURE_PORT;
+                       setflags(NFSEXP_INSECURE_PORT, active, ep);
                else if (!strcmp(opt, "sync"))
-                       ep->e_flags &= ~NFSEXP_ASYNC;
+                       clearflags(NFSEXP_ASYNC, active, ep);
                else if (!strcmp(opt, "async"))
-                       ep->e_flags |= NFSEXP_ASYNC;
+                       setflags(NFSEXP_ASYNC, active, ep);
                else if (!strcmp(opt, "nohide"))
-                       ep->e_flags |= NFSEXP_NOHIDE;
+                       setflags(NFSEXP_NOHIDE, active, ep);
                else if (!strcmp(opt, "hide"))
-                       ep->e_flags &= ~NFSEXP_NOHIDE;
+                       clearflags(NFSEXP_NOHIDE, active, ep);
                else if (!strcmp(opt, "crossmnt"))
-                       ep->e_flags |= NFSEXP_CROSSMOUNT;
+                       setflags(NFSEXP_CROSSMOUNT, active, ep);
                else if (!strcmp(opt, "nocrossmnt"))
-                       ep->e_flags &= ~NFSEXP_CROSSMOUNT;
+                       clearflags(NFSEXP_CROSSMOUNT, active, ep);
                else if (!strcmp(opt, "wdelay"))
-                       ep->e_flags |= NFSEXP_GATHERED_WRITES;
+                       setflags(NFSEXP_GATHERED_WRITES, active, ep);
                else if (!strcmp(opt, "no_wdelay"))
-                       ep->e_flags &= ~NFSEXP_GATHERED_WRITES;
+                       clearflags(NFSEXP_GATHERED_WRITES, active, ep);
                else if (strcmp(opt, "root_squash") == 0)
-                       ep->e_flags |= NFSEXP_ROOTSQUASH;
+                       setflags(NFSEXP_ROOTSQUASH, active, ep);
                else if (!strcmp(opt, "no_root_squash"))
-                       ep->e_flags &= ~NFSEXP_ROOTSQUASH;
+                       clearflags(NFSEXP_ROOTSQUASH, active, ep);
                else if (strcmp(opt, "all_squash") == 0)
-                       ep->e_flags |= NFSEXP_ALLSQUASH;
+                       setflags(NFSEXP_ALLSQUASH, active, ep);
                else if (strcmp(opt, "no_all_squash") == 0)
-                       ep->e_flags &= ~NFSEXP_ALLSQUASH;
+                       clearflags(NFSEXP_ALLSQUASH, active, ep);
                else if (strcmp(opt, "subtree_check") == 0) {
                        had_subtree_opt = 1;
-                       ep->e_flags &= ~NFSEXP_NOSUBTREECHECK;
+                       clearflags(NFSEXP_NOSUBTREECHECK, active, ep);
                } else if (strcmp(opt, "no_subtree_check") == 0) {
                        had_subtree_opt = 1;
-                       ep->e_flags |= NFSEXP_NOSUBTREECHECK;
+                       setflags(NFSEXP_NOSUBTREECHECK, active, ep);
                } else if (strcmp(opt, "auth_nlm") == 0)
-                       ep->e_flags &= ~NFSEXP_NOAUTHNLM;
+                       clearflags(NFSEXP_NOAUTHNLM, active, ep);
                else if (strcmp(opt, "no_auth_nlm") == 0)
-                       ep->e_flags |= NFSEXP_NOAUTHNLM;
+                       setflags(NFSEXP_NOAUTHNLM, active, ep);
                else if (strcmp(opt, "secure_locks") == 0)
-                       ep->e_flags &= ~NFSEXP_NOAUTHNLM;
+                       clearflags(NFSEXP_NOAUTHNLM, active, ep);
                else if (strcmp(opt, "insecure_locks") == 0)
-                       ep->e_flags |= NFSEXP_NOAUTHNLM;
+                       setflags(NFSEXP_NOAUTHNLM, active, ep);
                else if (strcmp(opt, "acl") == 0)
-                       ep->e_flags &= ~NFSEXP_NOACL;
+                       clearflags(NFSEXP_NOACL, active, ep);
                else if (strcmp(opt, "no_acl") == 0)
-                       ep->e_flags |= NFSEXP_NOACL;
-               else if (strncmp(opt, "mapping=", 8) == 0)
-                       ep->e_maptype = parsemaptype(opt+8);
-               else if (strcmp(opt, "map_identity") == 0)      /* old style */
-                       ep->e_maptype = CLE_MAP_IDENT;
-               else if (strcmp(opt, "map_daemon") == 0)        /* old style */
-                       ep->e_maptype = CLE_MAP_UGIDD;
+                       setflags(NFSEXP_NOACL, active, ep);
                else if (strncmp(opt, "anonuid=", 8) == 0) {
                        char *oe;
                        ep->e_anonuid = strtol(opt+8, &oe, 10);
@@ -484,13 +590,13 @@ bad_option:
                        char *oe;
                        if (strcmp(opt+5, "root") == 0) {
                                ep->e_fsid = 0;
-                               ep->e_flags |= NFSEXP_FSID;
+                               setflags(NFSEXP_FSID, active, ep);
                        } else {
                                ep->e_fsid = strtoul(opt+5, &oe, 0);
                                if (opt[5]!='\0' && *oe == '\0') 
-                                       ep->e_flags |= NFSEXP_FSID;
+                                       setflags(NFSEXP_FSID, active, ep);
                                else if (valid_uuid(opt+5))
-                                       ep->e_uuid = strdup(opt+7);
+                                       ep->e_uuid = strdup(opt+5);
                                else {
                                        xlog(L_ERROR, "%s: %d: bad fsid \"%s\"\n",
                                             flname, flline, opt);      
@@ -522,10 +628,14 @@ bad_option:
                } else if (strncmp(opt, "replicas=", 9) == 0) {
                        ep->e_fslocmethod = FSLOC_REPLICA;
                        ep->e_fslocdata = strdup(opt+9);
+               } else if (strncmp(opt, "sec=", 4) == 0) {
+                       active = parse_flavors(opt+4, ep);
+                       if (!active)
+                               goto bad_option;
                } else {
                        xlog(L_ERROR, "%s:%d: unknown keyword \"%s\"\n",
                                        flname, flline, opt);
-                       ep->e_flags |= NFSEXP_ALLSQUASH | NFSEXP_READONLY;
+                       setflags(NFSEXP_ALLSQUASH | NFSEXP_READONLY, active, ep);
                        goto bad_option;
                }
                free(opt);
@@ -533,6 +643,9 @@ bad_option:
                        cp++;
        }
 
+       if (ep->e_secinfo[0].flav == NULL)
+               secinfo_addflavor(find_flavor("sys"), ep);
+       fix_pseudoflavor_flags(ep);
        ep->e_squids = squids;
        ep->e_sqgids = sqgids;
        ep->e_nsquids = nsquids;
@@ -624,19 +737,6 @@ parsenum(char **cpp)
        return num;
 }
 
-static int
-parsemaptype(char *type)
-{
-       if (!strcmp(type, "identity"))
-               return CLE_MAP_IDENT;
-       if (!strcmp(type, "ugidd"))
-               return CLE_MAP_UGIDD;
-       if (!strcmp(type, "file"))
-               return CLE_MAP_FILE;
-       syntaxerr("invalid map type");
-       return CLE_MAP_IDENT;   /* default */
-}
-
 static int
 getpath(char *path, int len)
 {
@@ -662,4 +762,35 @@ syntaxerr(char *msg)
        xlog(L_ERROR, "%s:%d: syntax error: %s",
                        efname, efp?efp->x_line:0, msg);
 }
-
+struct export_features *get_export_features(void)
+{
+       static char *path = "/proc/fs/nfsd/export_features";
+       static struct export_features ef;
+       static int cached = 0;
+       char buf[50];
+       int c;
+       int fd;
+
+       if (cached)
+               return &ef;
+
+       ef.flags = NFSEXP_OLDFLAGS;
+       ef.secinfo_flags = NFSEXP_OLD_SECINFO_FLAGS;
+
+       fd = open(path, O_RDONLY);
+       if (fd == -1)
+               goto good;
+       c = read(fd, buf, 50);
+       close(fd);
+       if (c == -1)
+               goto err;
+       c = sscanf(buf, "%x %x", &ef.flags, &ef.secinfo_flags);
+       if (c != 2)
+               goto err;
+good:
+       cached = 1;
+       return &ef;
+err:
+       xlog(L_WARNING, "unexpected error reading %s", path);
+       return &ef;
+}