Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Process version 3 NFSACL requests.
0004  *
0005  * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
0006  */
0007 
0008 #include "nfsd.h"
0009 /* FIXME: nfsacl.h is a broken header */
0010 #include <linux/nfsacl.h>
0011 #include <linux/gfp.h>
0012 #include "cache.h"
0013 #include "xdr3.h"
0014 #include "vfs.h"
0015 
0016 /*
0017  * NULL call.
0018  */
0019 static __be32
0020 nfsd3_proc_null(struct svc_rqst *rqstp)
0021 {
0022     return rpc_success;
0023 }
0024 
0025 /*
0026  * Get the Access and/or Default ACL of a file.
0027  */
0028 static __be32 nfsd3_proc_getacl(struct svc_rqst *rqstp)
0029 {
0030     struct nfsd3_getaclargs *argp = rqstp->rq_argp;
0031     struct nfsd3_getaclres *resp = rqstp->rq_resp;
0032     struct posix_acl *acl;
0033     struct inode *inode;
0034     svc_fh *fh;
0035 
0036     fh = fh_copy(&resp->fh, &argp->fh);
0037     resp->status = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
0038     if (resp->status != nfs_ok)
0039         goto out;
0040 
0041     inode = d_inode(fh->fh_dentry);
0042 
0043     if (argp->mask & ~NFS_ACL_MASK) {
0044         resp->status = nfserr_inval;
0045         goto out;
0046     }
0047     resp->mask = argp->mask;
0048 
0049     if (resp->mask & (NFS_ACL|NFS_ACLCNT)) {
0050         acl = get_acl(inode, ACL_TYPE_ACCESS);
0051         if (acl == NULL) {
0052             /* Solaris returns the inode's minimum ACL. */
0053             acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
0054         }
0055         if (IS_ERR(acl)) {
0056             resp->status = nfserrno(PTR_ERR(acl));
0057             goto fail;
0058         }
0059         resp->acl_access = acl;
0060     }
0061     if (resp->mask & (NFS_DFACL|NFS_DFACLCNT)) {
0062         /* Check how Solaris handles requests for the Default ACL
0063            of a non-directory! */
0064         acl = get_acl(inode, ACL_TYPE_DEFAULT);
0065         if (IS_ERR(acl)) {
0066             resp->status = nfserrno(PTR_ERR(acl));
0067             goto fail;
0068         }
0069         resp->acl_default = acl;
0070     }
0071 
0072     /* resp->acl_{access,default} are released in nfs3svc_release_getacl. */
0073 out:
0074     return rpc_success;
0075 
0076 fail:
0077     posix_acl_release(resp->acl_access);
0078     posix_acl_release(resp->acl_default);
0079     goto out;
0080 }
0081 
0082 /*
0083  * Set the Access and/or Default ACL of a file.
0084  */
0085 static __be32 nfsd3_proc_setacl(struct svc_rqst *rqstp)
0086 {
0087     struct nfsd3_setaclargs *argp = rqstp->rq_argp;
0088     struct nfsd3_attrstat *resp = rqstp->rq_resp;
0089     struct inode *inode;
0090     svc_fh *fh;
0091     int error;
0092 
0093     fh = fh_copy(&resp->fh, &argp->fh);
0094     resp->status = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
0095     if (resp->status != nfs_ok)
0096         goto out;
0097 
0098     inode = d_inode(fh->fh_dentry);
0099 
0100     error = fh_want_write(fh);
0101     if (error)
0102         goto out_errno;
0103 
0104     inode_lock(inode);
0105 
0106     error = set_posix_acl(&init_user_ns, inode, ACL_TYPE_ACCESS,
0107                   argp->acl_access);
0108     if (error)
0109         goto out_drop_lock;
0110     error = set_posix_acl(&init_user_ns, inode, ACL_TYPE_DEFAULT,
0111                   argp->acl_default);
0112 
0113 out_drop_lock:
0114     inode_unlock(inode);
0115     fh_drop_write(fh);
0116 out_errno:
0117     resp->status = nfserrno(error);
0118 out:
0119     /* argp->acl_{access,default} may have been allocated in
0120        nfs3svc_decode_setaclargs. */
0121     posix_acl_release(argp->acl_access);
0122     posix_acl_release(argp->acl_default);
0123     return rpc_success;
0124 }
0125 
0126 /*
0127  * XDR decode functions
0128  */
0129 
0130 static bool
0131 nfs3svc_decode_getaclargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
0132 {
0133     struct nfsd3_getaclargs *args = rqstp->rq_argp;
0134 
0135     if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
0136         return false;
0137     if (xdr_stream_decode_u32(xdr, &args->mask) < 0)
0138         return false;
0139 
0140     return true;
0141 }
0142 
0143 static bool
0144 nfs3svc_decode_setaclargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
0145 {
0146     struct nfsd3_setaclargs *argp = rqstp->rq_argp;
0147 
0148     if (!svcxdr_decode_nfs_fh3(xdr, &argp->fh))
0149         return false;
0150     if (xdr_stream_decode_u32(xdr, &argp->mask) < 0)
0151         return false;
0152     if (argp->mask & ~NFS_ACL_MASK)
0153         return false;
0154     if (!nfs_stream_decode_acl(xdr, NULL, (argp->mask & NFS_ACL) ?
0155                    &argp->acl_access : NULL))
0156         return false;
0157     if (!nfs_stream_decode_acl(xdr, NULL, (argp->mask & NFS_DFACL) ?
0158                    &argp->acl_default : NULL))
0159         return false;
0160 
0161     return true;
0162 }
0163 
0164 /*
0165  * XDR encode functions
0166  */
0167 
0168 /* GETACL */
0169 static bool
0170 nfs3svc_encode_getaclres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
0171 {
0172     struct nfsd3_getaclres *resp = rqstp->rq_resp;
0173     struct dentry *dentry = resp->fh.fh_dentry;
0174     struct kvec *head = rqstp->rq_res.head;
0175     struct inode *inode;
0176     unsigned int base;
0177     int n;
0178     int w;
0179 
0180     if (!svcxdr_encode_nfsstat3(xdr, resp->status))
0181         return false;
0182     switch (resp->status) {
0183     case nfs_ok:
0184         inode = d_inode(dentry);
0185         if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
0186             return false;
0187         if (xdr_stream_encode_u32(xdr, resp->mask) < 0)
0188             return false;
0189 
0190         base = (char *)xdr->p - (char *)head->iov_base;
0191 
0192         rqstp->rq_res.page_len = w = nfsacl_size(
0193             (resp->mask & NFS_ACL)   ? resp->acl_access  : NULL,
0194             (resp->mask & NFS_DFACL) ? resp->acl_default : NULL);
0195         while (w > 0) {
0196             if (!*(rqstp->rq_next_page++))
0197                 return false;
0198             w -= PAGE_SIZE;
0199         }
0200 
0201         n = nfsacl_encode(&rqstp->rq_res, base, inode,
0202                   resp->acl_access,
0203                   resp->mask & NFS_ACL, 0);
0204         if (n > 0)
0205             n = nfsacl_encode(&rqstp->rq_res, base + n, inode,
0206                       resp->acl_default,
0207                       resp->mask & NFS_DFACL,
0208                       NFS_ACL_DEFAULT);
0209         if (n <= 0)
0210             return false;
0211         break;
0212     default:
0213         if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
0214             return false;
0215     }
0216 
0217     return true;
0218 }
0219 
0220 /* SETACL */
0221 static bool
0222 nfs3svc_encode_setaclres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
0223 {
0224     struct nfsd3_attrstat *resp = rqstp->rq_resp;
0225 
0226     return svcxdr_encode_nfsstat3(xdr, resp->status) &&
0227         svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh);
0228 }
0229 
0230 /*
0231  * XDR release functions
0232  */
0233 static void nfs3svc_release_getacl(struct svc_rqst *rqstp)
0234 {
0235     struct nfsd3_getaclres *resp = rqstp->rq_resp;
0236 
0237     fh_put(&resp->fh);
0238     posix_acl_release(resp->acl_access);
0239     posix_acl_release(resp->acl_default);
0240 }
0241 
0242 struct nfsd3_voidargs { int dummy; };
0243 
0244 #define ST 1        /* status*/
0245 #define AT 21       /* attributes */
0246 #define pAT (1+AT)  /* post attributes - conditional */
0247 #define ACL (1+NFS_ACL_MAX_ENTRIES*3)  /* Access Control List */
0248 
0249 static const struct svc_procedure nfsd_acl_procedures3[3] = {
0250     [ACLPROC3_NULL] = {
0251         .pc_func = nfsd3_proc_null,
0252         .pc_decode = nfssvc_decode_voidarg,
0253         .pc_encode = nfssvc_encode_voidres,
0254         .pc_argsize = sizeof(struct nfsd_voidargs),
0255         .pc_ressize = sizeof(struct nfsd_voidres),
0256         .pc_cachetype = RC_NOCACHE,
0257         .pc_xdrressize = ST,
0258         .pc_name = "NULL",
0259     },
0260     [ACLPROC3_GETACL] = {
0261         .pc_func = nfsd3_proc_getacl,
0262         .pc_decode = nfs3svc_decode_getaclargs,
0263         .pc_encode = nfs3svc_encode_getaclres,
0264         .pc_release = nfs3svc_release_getacl,
0265         .pc_argsize = sizeof(struct nfsd3_getaclargs),
0266         .pc_ressize = sizeof(struct nfsd3_getaclres),
0267         .pc_cachetype = RC_NOCACHE,
0268         .pc_xdrressize = ST+1+2*(1+ACL),
0269         .pc_name = "GETACL",
0270     },
0271     [ACLPROC3_SETACL] = {
0272         .pc_func = nfsd3_proc_setacl,
0273         .pc_decode = nfs3svc_decode_setaclargs,
0274         .pc_encode = nfs3svc_encode_setaclres,
0275         .pc_release = nfs3svc_release_fhandle,
0276         .pc_argsize = sizeof(struct nfsd3_setaclargs),
0277         .pc_ressize = sizeof(struct nfsd3_attrstat),
0278         .pc_cachetype = RC_NOCACHE,
0279         .pc_xdrressize = ST+pAT,
0280         .pc_name = "SETACL",
0281     },
0282 };
0283 
0284 static unsigned int nfsd_acl_count3[ARRAY_SIZE(nfsd_acl_procedures3)];
0285 const struct svc_version nfsd_acl_version3 = {
0286     .vs_vers    = 3,
0287     .vs_nproc   = 3,
0288     .vs_proc    = nfsd_acl_procedures3,
0289     .vs_count   = nfsd_acl_count3,
0290     .vs_dispatch    = nfsd_dispatch,
0291     .vs_xdrsize = NFS3_SVC_XDRSIZE,
0292 };
0293