nfs3acl.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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_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. fh_lock(fh);
  91. error = set_posix_acl(inode, ACL_TYPE_ACCESS, argp->acl_access);
  92. if (error)
  93. goto out_drop_lock;
  94. error = set_posix_acl(inode, ACL_TYPE_DEFAULT, argp->acl_default);
  95. out_drop_lock:
  96. fh_unlock(fh);
  97. fh_drop_write(fh);
  98. out_errno:
  99. resp->status = nfserrno(error);
  100. out:
  101. /* argp->acl_{access,default} may have been allocated in
  102. nfs3svc_decode_setaclargs. */
  103. posix_acl_release(argp->acl_access);
  104. posix_acl_release(argp->acl_default);
  105. return rpc_success;
  106. }
  107. /*
  108. * XDR decode functions
  109. */
  110. static int nfs3svc_decode_getaclargs(struct svc_rqst *rqstp, __be32 *p)
  111. {
  112. struct nfsd3_getaclargs *args = rqstp->rq_argp;
  113. p = nfs3svc_decode_fh(p, &args->fh);
  114. if (!p)
  115. return 0;
  116. args->mask = ntohl(*p); p++;
  117. return xdr_argsize_check(rqstp, p);
  118. }
  119. static int nfs3svc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p)
  120. {
  121. struct nfsd3_setaclargs *args = rqstp->rq_argp;
  122. struct kvec *head = rqstp->rq_arg.head;
  123. unsigned int base;
  124. int n;
  125. p = nfs3svc_decode_fh(p, &args->fh);
  126. if (!p)
  127. return 0;
  128. args->mask = ntohl(*p++);
  129. if (args->mask & ~NFS_ACL_MASK ||
  130. !xdr_argsize_check(rqstp, p))
  131. return 0;
  132. base = (char *)p - (char *)head->iov_base;
  133. n = nfsacl_decode(&rqstp->rq_arg, base, NULL,
  134. (args->mask & NFS_ACL) ?
  135. &args->acl_access : NULL);
  136. if (n > 0)
  137. n = nfsacl_decode(&rqstp->rq_arg, base + n, NULL,
  138. (args->mask & NFS_DFACL) ?
  139. &args->acl_default : NULL);
  140. return (n > 0);
  141. }
  142. /*
  143. * XDR encode functions
  144. */
  145. /* GETACL */
  146. static int nfs3svc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p)
  147. {
  148. struct nfsd3_getaclres *resp = rqstp->rq_resp;
  149. struct dentry *dentry = resp->fh.fh_dentry;
  150. *p++ = resp->status;
  151. p = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh);
  152. if (resp->status == 0 && dentry && d_really_is_positive(dentry)) {
  153. struct inode *inode = d_inode(dentry);
  154. struct kvec *head = rqstp->rq_res.head;
  155. unsigned int base;
  156. int n;
  157. int w;
  158. *p++ = htonl(resp->mask);
  159. if (!xdr_ressize_check(rqstp, p))
  160. return 0;
  161. base = (char *)p - (char *)head->iov_base;
  162. rqstp->rq_res.page_len = w = nfsacl_size(
  163. (resp->mask & NFS_ACL) ? resp->acl_access : NULL,
  164. (resp->mask & NFS_DFACL) ? resp->acl_default : NULL);
  165. while (w > 0) {
  166. if (!*(rqstp->rq_next_page++))
  167. return 0;
  168. w -= PAGE_SIZE;
  169. }
  170. n = nfsacl_encode(&rqstp->rq_res, base, inode,
  171. resp->acl_access,
  172. resp->mask & NFS_ACL, 0);
  173. if (n > 0)
  174. n = nfsacl_encode(&rqstp->rq_res, base + n, inode,
  175. resp->acl_default,
  176. resp->mask & NFS_DFACL,
  177. NFS_ACL_DEFAULT);
  178. if (n <= 0)
  179. return 0;
  180. } else
  181. if (!xdr_ressize_check(rqstp, p))
  182. return 0;
  183. return 1;
  184. }
  185. /* SETACL */
  186. static int nfs3svc_encode_setaclres(struct svc_rqst *rqstp, __be32 *p)
  187. {
  188. struct nfsd3_attrstat *resp = rqstp->rq_resp;
  189. *p++ = resp->status;
  190. p = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh);
  191. return xdr_ressize_check(rqstp, p);
  192. }
  193. /*
  194. * XDR release functions
  195. */
  196. static void nfs3svc_release_getacl(struct svc_rqst *rqstp)
  197. {
  198. struct nfsd3_getaclres *resp = rqstp->rq_resp;
  199. fh_put(&resp->fh);
  200. posix_acl_release(resp->acl_access);
  201. posix_acl_release(resp->acl_default);
  202. }
  203. struct nfsd3_voidargs { int dummy; };
  204. #define ST 1 /* status*/
  205. #define AT 21 /* attributes */
  206. #define pAT (1+AT) /* post attributes - conditional */
  207. #define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
  208. static const struct svc_procedure nfsd_acl_procedures3[3] = {
  209. [ACLPROC3_NULL] = {
  210. .pc_func = nfsd3_proc_null,
  211. .pc_decode = nfs3svc_decode_voidarg,
  212. .pc_encode = nfs3svc_encode_voidres,
  213. .pc_argsize = sizeof(struct nfsd3_voidargs),
  214. .pc_ressize = sizeof(struct nfsd3_voidargs),
  215. .pc_cachetype = RC_NOCACHE,
  216. .pc_xdrressize = ST,
  217. },
  218. [ACLPROC3_GETACL] = {
  219. .pc_func = nfsd3_proc_getacl,
  220. .pc_decode = nfs3svc_decode_getaclargs,
  221. .pc_encode = nfs3svc_encode_getaclres,
  222. .pc_release = nfs3svc_release_getacl,
  223. .pc_argsize = sizeof(struct nfsd3_getaclargs),
  224. .pc_ressize = sizeof(struct nfsd3_getaclres),
  225. .pc_cachetype = RC_NOCACHE,
  226. .pc_xdrressize = ST+1+2*(1+ACL),
  227. },
  228. [ACLPROC3_SETACL] = {
  229. .pc_func = nfsd3_proc_setacl,
  230. .pc_decode = nfs3svc_decode_setaclargs,
  231. .pc_encode = nfs3svc_encode_setaclres,
  232. .pc_release = nfs3svc_release_fhandle,
  233. .pc_argsize = sizeof(struct nfsd3_setaclargs),
  234. .pc_ressize = sizeof(struct nfsd3_attrstat),
  235. .pc_cachetype = RC_NOCACHE,
  236. .pc_xdrressize = ST+pAT,
  237. },
  238. };
  239. static unsigned int nfsd_acl_count3[ARRAY_SIZE(nfsd_acl_procedures3)];
  240. const struct svc_version nfsd_acl_version3 = {
  241. .vs_vers = 3,
  242. .vs_nproc = 3,
  243. .vs_proc = nfsd_acl_procedures3,
  244. .vs_count = nfsd_acl_count3,
  245. .vs_dispatch = nfsd_dispatch,
  246. .vs_xdrsize = NFS3_SVC_XDRSIZE,
  247. };