nfs3acl.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. #define RETURN_STATUS(st) { resp->status = (st); return (st); }
  15. /*
  16. * NULL call.
  17. */
  18. static __be32
  19. nfsd3_proc_null(struct svc_rqst *rqstp)
  20. {
  21. return nfs_ok;
  22. }
  23. /*
  24. * Get the Access and/or Default ACL of a file.
  25. */
  26. static __be32 nfsd3_proc_getacl(struct svc_rqst *rqstp)
  27. {
  28. struct nfsd3_getaclargs *argp = rqstp->rq_argp;
  29. struct nfsd3_getaclres *resp = rqstp->rq_resp;
  30. struct posix_acl *acl;
  31. struct inode *inode;
  32. svc_fh *fh;
  33. __be32 nfserr = 0;
  34. fh = fh_copy(&resp->fh, &argp->fh);
  35. nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
  36. if (nfserr)
  37. RETURN_STATUS(nfserr);
  38. inode = d_inode(fh->fh_dentry);
  39. if (argp->mask & ~NFS_ACL_MASK)
  40. RETURN_STATUS(nfserr_inval);
  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. nfserr = 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. nfserr = 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. RETURN_STATUS(0);
  66. fail:
  67. posix_acl_release(resp->acl_access);
  68. posix_acl_release(resp->acl_default);
  69. RETURN_STATUS(nfserr);
  70. }
  71. /*
  72. * Set the Access and/or Default ACL of a file.
  73. */
  74. static __be32 nfsd3_proc_setacl(struct svc_rqst *rqstp)
  75. {
  76. struct nfsd3_setaclargs *argp = rqstp->rq_argp;
  77. struct nfsd3_attrstat *resp = rqstp->rq_resp;
  78. struct inode *inode;
  79. svc_fh *fh;
  80. __be32 nfserr = 0;
  81. int error;
  82. fh = fh_copy(&resp->fh, &argp->fh);
  83. nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
  84. if (nfserr)
  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. nfserr = 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_STATUS(nfserr);
  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 = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh);
  151. if (resp->status == 0 && dentry && d_really_is_positive(dentry)) {
  152. struct inode *inode = d_inode(dentry);
  153. struct kvec *head = rqstp->rq_res.head;
  154. unsigned int base;
  155. int n;
  156. int w;
  157. *p++ = htonl(resp->mask);
  158. if (!xdr_ressize_check(rqstp, p))
  159. return 0;
  160. base = (char *)p - (char *)head->iov_base;
  161. rqstp->rq_res.page_len = w = nfsacl_size(
  162. (resp->mask & NFS_ACL) ? resp->acl_access : NULL,
  163. (resp->mask & NFS_DFACL) ? resp->acl_default : NULL);
  164. while (w > 0) {
  165. if (!*(rqstp->rq_next_page++))
  166. return 0;
  167. w -= PAGE_SIZE;
  168. }
  169. n = nfsacl_encode(&rqstp->rq_res, base, inode,
  170. resp->acl_access,
  171. resp->mask & NFS_ACL, 0);
  172. if (n > 0)
  173. n = nfsacl_encode(&rqstp->rq_res, base + n, inode,
  174. resp->acl_default,
  175. resp->mask & NFS_DFACL,
  176. NFS_ACL_DEFAULT);
  177. if (n <= 0)
  178. return 0;
  179. } else
  180. if (!xdr_ressize_check(rqstp, p))
  181. return 0;
  182. return 1;
  183. }
  184. /* SETACL */
  185. static int nfs3svc_encode_setaclres(struct svc_rqst *rqstp, __be32 *p)
  186. {
  187. struct nfsd3_attrstat *resp = rqstp->rq_resp;
  188. p = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh);
  189. return xdr_ressize_check(rqstp, p);
  190. }
  191. /*
  192. * XDR release functions
  193. */
  194. static void nfs3svc_release_getacl(struct svc_rqst *rqstp)
  195. {
  196. struct nfsd3_getaclres *resp = rqstp->rq_resp;
  197. fh_put(&resp->fh);
  198. posix_acl_release(resp->acl_access);
  199. posix_acl_release(resp->acl_default);
  200. }
  201. #define nfs3svc_decode_voidargs NULL
  202. #define nfs3svc_release_void NULL
  203. #define nfsd3_setaclres nfsd3_attrstat
  204. #define nfsd3_voidres nfsd3_voidargs
  205. struct nfsd3_voidargs { int dummy; };
  206. #define PROC(name, argt, rest, relt, cache, respsize) \
  207. { \
  208. .pc_func = nfsd3_proc_##name, \
  209. .pc_decode = nfs3svc_decode_##argt##args, \
  210. .pc_encode = nfs3svc_encode_##rest##res, \
  211. .pc_release = nfs3svc_release_##relt, \
  212. .pc_argsize = sizeof(struct nfsd3_##argt##args), \
  213. .pc_ressize = sizeof(struct nfsd3_##rest##res), \
  214. .pc_cachetype = cache, \
  215. .pc_xdrressize = respsize, \
  216. }
  217. #define ST 1 /* status*/
  218. #define AT 21 /* attributes */
  219. #define pAT (1+AT) /* post attributes - conditional */
  220. #define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
  221. static const struct svc_procedure nfsd_acl_procedures3[] = {
  222. PROC(null, void, void, void, RC_NOCACHE, ST),
  223. PROC(getacl, getacl, getacl, getacl, RC_NOCACHE, ST+1+2*(1+ACL)),
  224. PROC(setacl, setacl, setacl, fhandle, RC_NOCACHE, ST+pAT),
  225. };
  226. static unsigned int nfsd_acl_count3[ARRAY_SIZE(nfsd_acl_procedures3)];
  227. const struct svc_version nfsd_acl_version3 = {
  228. .vs_vers = 3,
  229. .vs_nproc = 3,
  230. .vs_proc = nfsd_acl_procedures3,
  231. .vs_count = nfsd_acl_count3,
  232. .vs_dispatch = nfsd_dispatch,
  233. .vs_xdrsize = NFS3_SVC_XDRSIZE,
  234. };