nfs3acl.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Process version 3 NFSACL requests.
  4. *
  5. * Copyright (C) 2002-2003 Andreas Gruenbacher <[email protected]>
  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_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_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. goto out;
  71. }
  72. /*
  73. * Set the Access and/or Default ACL of a file.
  74. */
  75. static __be32 nfsd3_proc_setacl(struct svc_rqst *rqstp)
  76. {
  77. struct nfsd3_setaclargs *argp = rqstp->rq_argp;
  78. struct nfsd3_attrstat *resp = rqstp->rq_resp;
  79. struct inode *inode;
  80. svc_fh *fh;
  81. int error;
  82. fh = fh_copy(&resp->fh, &argp->fh);
  83. resp->status = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
  84. if (resp->status != nfs_ok)
  85. goto out;
  86. inode = d_inode(fh->fh_dentry);
  87. error = fh_want_write(fh);
  88. if (error)
  89. goto out_errno;
  90. inode_lock(inode);
  91. error = set_posix_acl(&init_user_ns, inode, ACL_TYPE_ACCESS,
  92. argp->acl_access);
  93. if (error)
  94. goto out_drop_lock;
  95. error = set_posix_acl(&init_user_ns, inode, ACL_TYPE_DEFAULT,
  96. argp->acl_default);
  97. out_drop_lock:
  98. inode_unlock(inode);
  99. fh_drop_write(fh);
  100. out_errno:
  101. resp->status = nfserrno(error);
  102. out:
  103. /* argp->acl_{access,default} may have been allocated in
  104. nfs3svc_decode_setaclargs. */
  105. posix_acl_release(argp->acl_access);
  106. posix_acl_release(argp->acl_default);
  107. return rpc_success;
  108. }
  109. /*
  110. * XDR decode functions
  111. */
  112. static bool
  113. nfs3svc_decode_getaclargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  114. {
  115. struct nfsd3_getaclargs *args = rqstp->rq_argp;
  116. if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
  117. return false;
  118. if (xdr_stream_decode_u32(xdr, &args->mask) < 0)
  119. return false;
  120. return true;
  121. }
  122. static bool
  123. nfs3svc_decode_setaclargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  124. {
  125. struct nfsd3_setaclargs *argp = rqstp->rq_argp;
  126. if (!svcxdr_decode_nfs_fh3(xdr, &argp->fh))
  127. return false;
  128. if (xdr_stream_decode_u32(xdr, &argp->mask) < 0)
  129. return false;
  130. if (argp->mask & ~NFS_ACL_MASK)
  131. return false;
  132. if (!nfs_stream_decode_acl(xdr, NULL, (argp->mask & NFS_ACL) ?
  133. &argp->acl_access : NULL))
  134. return false;
  135. if (!nfs_stream_decode_acl(xdr, NULL, (argp->mask & NFS_DFACL) ?
  136. &argp->acl_default : NULL))
  137. return false;
  138. return true;
  139. }
  140. /*
  141. * XDR encode functions
  142. */
  143. /* GETACL */
  144. static bool
  145. nfs3svc_encode_getaclres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  146. {
  147. struct nfsd3_getaclres *resp = rqstp->rq_resp;
  148. struct dentry *dentry = resp->fh.fh_dentry;
  149. struct inode *inode;
  150. if (!svcxdr_encode_nfsstat3(xdr, resp->status))
  151. return false;
  152. switch (resp->status) {
  153. case nfs_ok:
  154. inode = d_inode(dentry);
  155. if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
  156. return false;
  157. if (xdr_stream_encode_u32(xdr, resp->mask) < 0)
  158. return false;
  159. if (!nfs_stream_encode_acl(xdr, inode, resp->acl_access,
  160. resp->mask & NFS_ACL, 0))
  161. return false;
  162. if (!nfs_stream_encode_acl(xdr, inode, resp->acl_default,
  163. resp->mask & NFS_DFACL,
  164. NFS_ACL_DEFAULT))
  165. return false;
  166. break;
  167. default:
  168. if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
  169. return false;
  170. }
  171. return true;
  172. }
  173. /* SETACL */
  174. static bool
  175. nfs3svc_encode_setaclres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  176. {
  177. struct nfsd3_attrstat *resp = rqstp->rq_resp;
  178. return svcxdr_encode_nfsstat3(xdr, resp->status) &&
  179. svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh);
  180. }
  181. /*
  182. * XDR release functions
  183. */
  184. static void nfs3svc_release_getacl(struct svc_rqst *rqstp)
  185. {
  186. struct nfsd3_getaclres *resp = rqstp->rq_resp;
  187. fh_put(&resp->fh);
  188. posix_acl_release(resp->acl_access);
  189. posix_acl_release(resp->acl_default);
  190. }
  191. struct nfsd3_voidargs { int dummy; };
  192. #define ST 1 /* status*/
  193. #define AT 21 /* attributes */
  194. #define pAT (1+AT) /* post attributes - conditional */
  195. #define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
  196. static const struct svc_procedure nfsd_acl_procedures3[3] = {
  197. [ACLPROC3_NULL] = {
  198. .pc_func = nfsd3_proc_null,
  199. .pc_decode = nfssvc_decode_voidarg,
  200. .pc_encode = nfssvc_encode_voidres,
  201. .pc_argsize = sizeof(struct nfsd_voidargs),
  202. .pc_argzero = sizeof(struct nfsd_voidargs),
  203. .pc_ressize = sizeof(struct nfsd_voidres),
  204. .pc_cachetype = RC_NOCACHE,
  205. .pc_xdrressize = ST,
  206. .pc_name = "NULL",
  207. },
  208. [ACLPROC3_GETACL] = {
  209. .pc_func = nfsd3_proc_getacl,
  210. .pc_decode = nfs3svc_decode_getaclargs,
  211. .pc_encode = nfs3svc_encode_getaclres,
  212. .pc_release = nfs3svc_release_getacl,
  213. .pc_argsize = sizeof(struct nfsd3_getaclargs),
  214. .pc_argzero = sizeof(struct nfsd3_getaclargs),
  215. .pc_ressize = sizeof(struct nfsd3_getaclres),
  216. .pc_cachetype = RC_NOCACHE,
  217. .pc_xdrressize = ST+1+2*(1+ACL),
  218. .pc_name = "GETACL",
  219. },
  220. [ACLPROC3_SETACL] = {
  221. .pc_func = nfsd3_proc_setacl,
  222. .pc_decode = nfs3svc_decode_setaclargs,
  223. .pc_encode = nfs3svc_encode_setaclres,
  224. .pc_release = nfs3svc_release_fhandle,
  225. .pc_argsize = sizeof(struct nfsd3_setaclargs),
  226. .pc_argzero = sizeof(struct nfsd3_setaclargs),
  227. .pc_ressize = sizeof(struct nfsd3_attrstat),
  228. .pc_cachetype = RC_NOCACHE,
  229. .pc_xdrressize = ST+pAT,
  230. .pc_name = "SETACL",
  231. },
  232. };
  233. static unsigned int nfsd_acl_count3[ARRAY_SIZE(nfsd_acl_procedures3)];
  234. const struct svc_version nfsd_acl_version3 = {
  235. .vs_vers = 3,
  236. .vs_nproc = 3,
  237. .vs_proc = nfsd_acl_procedures3,
  238. .vs_count = nfsd_acl_count3,
  239. .vs_dispatch = nfsd_dispatch,
  240. .vs_xdrsize = NFS3_SVC_XDRSIZE,
  241. };