knfsd: nfsd4: return nfserr_wrongsec
[safe/jmp/linux-2.6] / fs / nfsd / export.c
index fbbbcc5..323cbdc 100644 (file)
@@ -1228,6 +1228,119 @@ exp_find(struct auth_domain *clp, int fsid_type, u32 *fsidv,
        return exp;
 }
 
+__be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp)
+{
+       struct exp_flavor_info *f;
+       struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
+
+       /* legacy gss-only clients are always OK: */
+       if (exp->ex_client == rqstp->rq_gssclient)
+               return 0;
+       /* ip-address based client; check sec= export option: */
+       for (f = exp->ex_flavors; f < end; f++) {
+               if (f->pseudoflavor == rqstp->rq_flavor)
+                       return 0;
+       }
+       /* defaults in absence of sec= options: */
+       if (exp->ex_nflavors == 0) {
+               if (rqstp->rq_flavor == RPC_AUTH_NULL ||
+                   rqstp->rq_flavor == RPC_AUTH_UNIX)
+                       return 0;
+       }
+       return nfserr_wrongsec;
+}
+
+/*
+ * Uses rq_client and rq_gssclient to find an export; uses rq_client (an
+ * auth_unix client) if it's available and has secinfo information;
+ * otherwise, will try to use rq_gssclient.
+ *
+ * Called from functions that handle requests; functions that do work on
+ * behalf of mountd are passed a single client name to use, and should
+ * use exp_get_by_name() or exp_find().
+ */
+struct svc_export *
+rqst_exp_get_by_name(struct svc_rqst *rqstp, struct vfsmount *mnt,
+               struct dentry *dentry)
+{
+       struct svc_export *gssexp, *exp = NULL;
+
+       if (rqstp->rq_client == NULL)
+               goto gss;
+
+       /* First try the auth_unix client: */
+       exp = exp_get_by_name(rqstp->rq_client, mnt, dentry,
+                                               &rqstp->rq_chandle);
+       if (PTR_ERR(exp) == -ENOENT)
+               goto gss;
+       if (IS_ERR(exp))
+               return exp;
+       /* If it has secinfo, assume there are no gss/... clients */
+       if (exp->ex_nflavors > 0)
+               return exp;
+gss:
+       /* Otherwise, try falling back on gss client */
+       if (rqstp->rq_gssclient == NULL)
+               return exp;
+       gssexp = exp_get_by_name(rqstp->rq_gssclient, mnt, dentry,
+                                               &rqstp->rq_chandle);
+       if (PTR_ERR(gssexp) == -ENOENT)
+               return exp;
+       if (exp && !IS_ERR(exp))
+               exp_put(exp);
+       return gssexp;
+}
+
+struct svc_export *
+rqst_exp_find(struct svc_rqst *rqstp, int fsid_type, u32 *fsidv)
+{
+       struct svc_export *gssexp, *exp = NULL;
+
+       if (rqstp->rq_client == NULL)
+               goto gss;
+
+       /* First try the auth_unix client: */
+       exp = exp_find(rqstp->rq_client, fsid_type, fsidv, &rqstp->rq_chandle);
+       if (PTR_ERR(exp) == -ENOENT)
+               goto gss;
+       if (IS_ERR(exp))
+               return exp;
+       /* If it has secinfo, assume there are no gss/... clients */
+       if (exp->ex_nflavors > 0)
+               return exp;
+gss:
+       /* Otherwise, try falling back on gss client */
+       if (rqstp->rq_gssclient == NULL)
+               return exp;
+       gssexp = exp_find(rqstp->rq_gssclient, fsid_type, fsidv,
+                                               &rqstp->rq_chandle);
+       if (PTR_ERR(gssexp) == -ENOENT)
+               return exp;
+       if (exp && !IS_ERR(exp))
+               exp_put(exp);
+       return gssexp;
+}
+
+struct svc_export *
+rqst_exp_parent(struct svc_rqst *rqstp, struct vfsmount *mnt,
+               struct dentry *dentry)
+{
+       struct svc_export *exp;
+
+       dget(dentry);
+       exp = rqst_exp_get_by_name(rqstp, mnt, dentry);
+
+       while (PTR_ERR(exp) == -ENOENT && !IS_ROOT(dentry)) {
+               struct dentry *parent;
+
+               parent = dget_parent(dentry);
+               dput(dentry);
+               dentry = parent;
+               exp = rqst_exp_get_by_name(rqstp, mnt, dentry);
+       }
+       dput(dentry);
+       return exp;
+}
 
 /*
  * Called when we need the filehandle for the root of the pseudofs,
@@ -1235,8 +1348,7 @@ exp_find(struct auth_domain *clp, int fsid_type, u32 *fsidv,
  * export point with fsid==0
  */
 __be32
-exp_pseudoroot(struct auth_domain *clp, struct svc_fh *fhp,
-              struct cache_req *creq)
+exp_pseudoroot(struct svc_rqst *rqstp, struct svc_fh *fhp)
 {
        struct svc_export *exp;
        __be32 rv;
@@ -1244,12 +1356,16 @@ exp_pseudoroot(struct auth_domain *clp, struct svc_fh *fhp,
 
        mk_fsid(FSID_NUM, fsidv, 0, 0, 0, NULL);
 
-       exp = exp_find(clp, FSID_NUM, fsidv, creq);
+       exp = rqst_exp_find(rqstp, FSID_NUM, fsidv);
        if (PTR_ERR(exp) == -ENOENT)
                return nfserr_perm;
        if (IS_ERR(exp))
                return nfserrno(PTR_ERR(exp));
        rv = fh_compose(fhp, exp, exp->ex_dentry, NULL);
+       if (rv)
+               goto out;
+       rv = check_nfsd_access(exp, rqstp);
+out:
        exp_put(exp);
        return rv;
 }