nfs3acl.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Process version 3 NFSACL requests.
  4. *
  5. * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
  6. */
  7. #include "nfsd.h"
  8. /* FIXME: nfsacl.h is a broken header */
  9. #include <linux/nfsacl.h>
  10. #include <linux/gfp.h>
  11. #include "cache.h"
  12. #include "xdr3.h"
  13. #include "vfs.h"
  14. /*
  15. * NULL call.
  16. */
  17. static __be32
  18. nfsd3_proc_null(struct svc_rqst *rqstp)
  19. {
  20. return rpc_success;
  21. }
  22. /*
  23. * Get the Access and/or Default ACL of a file.
  24. */
  25. static __be32 nfsd3_proc_getacl(struct svc_rqst *rqstp)
  26. {
  27. struct nfsd3_getaclargs *argp = rqstp->rq_argp;
  28. struct nfsd3_getaclres *resp = rqstp->rq_resp;
  29. struct posix_acl *acl;
  30. struct inode *inode;
  31. svc_fh *fh;
  32. fh = fh_copy(&resp->fh, &argp->fh);
  33. resp->status = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
  34. if (resp->status != nfs_ok)
  35. goto out;
  36. inode = d_inode(fh->fh_dentry);
  37. if (argp->mask & ~NFS_ACL_MASK) {
  38. resp->status = nfserr_inval;
  39. goto out;
  40. }
  41. resp->mask = argp->mask;
  42. if (resp->mask & (NFS_ACL|NFS_ACLCNT)) {
  43. acl = get_inode_acl(inode, ACL_TYPE_ACCESS);
  44. if (acl == NULL) {
  45. /* Solaris returns the inode's minimum ACL. */
  46. acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  47. }
  48. if (IS_ERR(acl)) {
  49. resp->status = nfserrno(PTR_ERR(acl));
  50. goto fail;
  51. }
  52. resp->acl_access = acl;
  53. }
  54. if (resp->mask & (NFS_DFACL|NFS_DFACLCNT)) {
  55. /* Check how Solaris handles requests for the Default ACL
  56. of a non-directory! */
  57. acl = get_inode_acl(inode, ACL_TYPE_DEFAULT);
  58. if (IS_ERR(acl)) {
  59. resp->status = nfserrno(PTR_ERR(acl));
  60. goto fail;
  61. }
  62. resp->acl_default = acl;
  63. }
  64. /* resp->acl_{access,default} are released in nfs3svc_release_getacl. */
  65. out:
  66. return rpc_success;
  67. fail:
  68. posix_acl_release(resp->acl_access);
  69. posix_acl_release(resp->acl_default);
  70. resp->acl_access = NULL;
  71. resp->acl_default = NULL;
  72. goto out;
  73. }
  74. /*
  75. * Set the Access and/or Default ACL of a file.
  76. */
  77. static __be32 nfsd3_proc_setacl(struct svc_rqst *rqstp)
  78. {
  79. struct nfsd3_setaclargs *argp = rqstp->rq_argp;
  80. struct nfsd3_attrstat *resp = rqstp->rq_resp;
  81. struct inode *inode;
  82. svc_fh *fh;
  83. int error;
  84. fh = fh_copy(&resp->fh, &argp->fh);
  85. resp->status = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
  86. if (resp->status != nfs_ok)
  87. goto out;
  88. inode = d_inode(fh->fh_dentry);
  89. error = fh_want_write(fh);
  90. if (error)
  91. goto out_errno;
  92. inode_lock(inode);
  93. error = set_posix_acl(&nop_mnt_idmap, fh->fh_dentry, ACL_TYPE_ACCESS,
  94. argp->acl_access);
  95. if (error)
  96. goto out_drop_lock;
  97. error = set_posix_acl(&nop_mnt_idmap, fh->fh_dentry, ACL_TYPE_DEFAULT,
  98. argp->acl_default);
  99. out_drop_lock:
  100. inode_unlock(inode);
  101. fh_drop_write(fh);
  102. out_errno:
  103. resp->status = nfserrno(error);
  104. out:
  105. /* argp->acl_{access,default} may have been allocated in
  106. nfs3svc_decode_setaclargs. */
  107. posix_acl_release(argp->acl_access);
  108. posix_acl_release(argp->acl_default);
  109. return rpc_success;
  110. }
  111. /*
  112. * XDR decode functions
  113. */
  114. static bool
  115. nfs3svc_decode_getaclargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  116. {
  117. struct nfsd3_getaclargs *args = rqstp->rq_argp;
  118. if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
  119. return false;
  120. if (xdr_stream_decode_u32(xdr, &args->mask) < 0)
  121. return false;
  122. return true;
  123. }
  124. static bool
  125. nfs3svc_decode_setaclargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  126. {
  127. struct nfsd3_setaclargs *argp = rqstp->rq_argp;
  128. if (!svcxdr_decode_nfs_fh3(xdr, &argp->fh))
  129. return false;
  130. if (xdr_stream_decode_u32(xdr, &argp->mask) < 0)
  131. return false;
  132. if (argp->mask & ~NFS_ACL_MASK)
  133. return false;
  134. if (!nfs_stream_decode_acl(xdr, NULL, (argp->mask & NFS_ACL) ?
  135. &argp->acl_access : NULL))
  136. return false;
  137. if (!nfs_stream_decode_acl(xdr, NULL, (argp->mask & NFS_DFACL) ?
  138. &argp->acl_default : NULL))
  139. return false;
  140. return true;
  141. }
  142. /*
  143. * XDR encode functions
  144. */
  145. /* GETACL */
  146. static bool
  147. nfs3svc_encode_getaclres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  148. {
  149. struct nfsd3_getaclres *resp = rqstp->rq_resp;
  150. struct dentry *dentry = resp->fh.fh_dentry;
  151. struct inode *inode;
  152. if (!svcxdr_encode_nfsstat3(xdr, resp->status))
  153. return false;
  154. switch (resp->status) {
  155. case nfs_ok:
  156. inode = d_inode(dentry);
  157. if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
  158. return false;
  159. if (xdr_stream_encode_u32(xdr, resp->mask) < 0)
  160. return false;
  161. if (!nfs_stream_encode_acl(xdr, inode, resp->acl_access,
  162. resp->mask & NFS_ACL, 0))
  163. return false;
  164. if (!nfs_stream_encode_acl(xdr, inode, resp->acl_default,
  165. resp->mask & NFS_DFACL,
  166. NFS_ACL_DEFAULT))
  167. return false;
  168. break;
  169. default:
  170. if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
  171. return false;
  172. }
  173. return true;
  174. }
  175. /* SETACL */
  176. static bool
  177. nfs3svc_encode_setaclres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  178. {
  179. struct nfsd3_attrstat *resp = rqstp->rq_resp;
  180. return svcxdr_encode_nfsstat3(xdr, resp->status) &&
  181. svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh);
  182. }
  183. /*
  184. * XDR release functions
  185. */
  186. static void nfs3svc_release_getacl(struct svc_rqst *rqstp)
  187. {
  188. struct nfsd3_getaclres *resp = rqstp->rq_resp;
  189. fh_put(&resp->fh);
  190. posix_acl_release(resp->acl_access);
  191. posix_acl_release(resp->acl_default);
  192. }
  193. #define ST 1 /* status*/
  194. #define AT 21 /* attributes */
  195. #define pAT (1+AT) /* post attributes - conditional */
  196. #define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
  197. static const struct svc_procedure nfsd_acl_procedures3[3] = {
  198. [ACLPROC3_NULL] = {
  199. .pc_func = nfsd3_proc_null,
  200. .pc_decode = nfssvc_decode_voidarg,
  201. .pc_encode = nfssvc_encode_voidres,
  202. .pc_argsize = sizeof(struct nfsd_voidargs),
  203. .pc_argzero = sizeof(struct nfsd_voidargs),
  204. .pc_ressize = sizeof(struct nfsd_voidres),
  205. .pc_cachetype = RC_NOCACHE,
  206. .pc_xdrressize = ST,
  207. .pc_name = "NULL",
  208. },
  209. [ACLPROC3_GETACL] = {
  210. .pc_func = nfsd3_proc_getacl,
  211. .pc_decode = nfs3svc_decode_getaclargs,
  212. .pc_encode = nfs3svc_encode_getaclres,
  213. .pc_release = nfs3svc_release_getacl,
  214. .pc_argsize = sizeof(struct nfsd3_getaclargs),
  215. .pc_argzero = sizeof(struct nfsd3_getaclargs),
  216. .pc_ressize = sizeof(struct nfsd3_getaclres),
  217. .pc_cachetype = RC_NOCACHE,
  218. .pc_xdrressize = ST+1+2*(1+ACL),
  219. .pc_name = "GETACL",
  220. },
  221. [ACLPROC3_SETACL] = {
  222. .pc_func = nfsd3_proc_setacl,
  223. .pc_decode = nfs3svc_decode_setaclargs,
  224. .pc_encode = nfs3svc_encode_setaclres,
  225. .pc_release = nfs3svc_release_fhandle,
  226. .pc_argsize = sizeof(struct nfsd3_setaclargs),
  227. .pc_argzero = sizeof(struct nfsd3_setaclargs),
  228. .pc_ressize = sizeof(struct nfsd3_attrstat),
  229. .pc_cachetype = RC_NOCACHE,
  230. .pc_xdrressize = ST+pAT,
  231. .pc_name = "SETACL",
  232. },
  233. };
  234. static DEFINE_PER_CPU_ALIGNED(unsigned long,
  235. nfsd_acl_count3[ARRAY_SIZE(nfsd_acl_procedures3)]);
  236. const struct svc_version nfsd_acl_version3 = {
  237. .vs_vers = 3,
  238. .vs_nproc = ARRAY_SIZE(nfsd_acl_procedures3),
  239. .vs_proc = nfsd_acl_procedures3,
  240. .vs_count = nfsd_acl_count3,
  241. .vs_dispatch = nfsd_dispatch,
  242. .vs_xdrsize = NFS3_SVC_XDRSIZE,
  243. };