nfs2acl.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Process version 2 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 NFSDDBG_FACILITY NFSDDBG_PROC
  15. /*
  16. * NULL call.
  17. */
  18. static __be32
  19. nfsacld_proc_null(struct svc_rqst *rqstp)
  20. {
  21. return rpc_success;
  22. }
  23. /*
  24. * Get the Access and/or Default ACL of a file.
  25. */
  26. static __be32 nfsacld_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. dprintk("nfsd: GETACL(2acl) %s\n", SVCFH_fmt(&argp->fh));
  34. fh = fh_copy(&resp->fh, &argp->fh);
  35. resp->status = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
  36. if (resp->status != nfs_ok)
  37. goto out;
  38. inode = d_inode(fh->fh_dentry);
  39. if (argp->mask & ~NFS_ACL_MASK) {
  40. resp->status = nfserr_inval;
  41. goto out;
  42. }
  43. resp->mask = argp->mask;
  44. resp->status = fh_getattr(fh, &resp->stat);
  45. if (resp->status != nfs_ok)
  46. goto out;
  47. if (resp->mask & (NFS_ACL|NFS_ACLCNT)) {
  48. acl = get_inode_acl(inode, ACL_TYPE_ACCESS);
  49. if (acl == NULL) {
  50. /* Solaris returns the inode's minimum ACL. */
  51. acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  52. }
  53. if (IS_ERR(acl)) {
  54. resp->status = nfserrno(PTR_ERR(acl));
  55. goto fail;
  56. }
  57. resp->acl_access = acl;
  58. }
  59. if (resp->mask & (NFS_DFACL|NFS_DFACLCNT)) {
  60. /* Check how Solaris handles requests for the Default ACL
  61. of a non-directory! */
  62. acl = get_inode_acl(inode, ACL_TYPE_DEFAULT);
  63. if (IS_ERR(acl)) {
  64. resp->status = nfserrno(PTR_ERR(acl));
  65. goto fail;
  66. }
  67. resp->acl_default = acl;
  68. }
  69. /* resp->acl_{access,default} are released in nfssvc_release_getacl. */
  70. out:
  71. return rpc_success;
  72. fail:
  73. posix_acl_release(resp->acl_access);
  74. posix_acl_release(resp->acl_default);
  75. resp->acl_access = NULL;
  76. resp->acl_default = NULL;
  77. goto out;
  78. }
  79. /*
  80. * Set the Access and/or Default ACL of a file.
  81. */
  82. static __be32 nfsacld_proc_setacl(struct svc_rqst *rqstp)
  83. {
  84. struct nfsd3_setaclargs *argp = rqstp->rq_argp;
  85. struct nfsd_attrstat *resp = rqstp->rq_resp;
  86. struct inode *inode;
  87. svc_fh *fh;
  88. int error;
  89. dprintk("nfsd: SETACL(2acl) %s\n", SVCFH_fmt(&argp->fh));
  90. fh = fh_copy(&resp->fh, &argp->fh);
  91. resp->status = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
  92. if (resp->status != nfs_ok)
  93. goto out;
  94. inode = d_inode(fh->fh_dentry);
  95. error = fh_want_write(fh);
  96. if (error)
  97. goto out_errno;
  98. inode_lock(inode);
  99. error = set_posix_acl(&nop_mnt_idmap, fh->fh_dentry, ACL_TYPE_ACCESS,
  100. argp->acl_access);
  101. if (error)
  102. goto out_drop_lock;
  103. error = set_posix_acl(&nop_mnt_idmap, fh->fh_dentry, ACL_TYPE_DEFAULT,
  104. argp->acl_default);
  105. if (error)
  106. goto out_drop_lock;
  107. inode_unlock(inode);
  108. fh_drop_write(fh);
  109. resp->status = fh_getattr(fh, &resp->stat);
  110. out:
  111. /* argp->acl_{access,default} may have been allocated in
  112. nfssvc_decode_setaclargs. */
  113. posix_acl_release(argp->acl_access);
  114. posix_acl_release(argp->acl_default);
  115. return rpc_success;
  116. out_drop_lock:
  117. inode_unlock(inode);
  118. fh_drop_write(fh);
  119. out_errno:
  120. resp->status = nfserrno(error);
  121. goto out;
  122. }
  123. /*
  124. * Check file attributes
  125. */
  126. static __be32 nfsacld_proc_getattr(struct svc_rqst *rqstp)
  127. {
  128. struct nfsd_fhandle *argp = rqstp->rq_argp;
  129. struct nfsd_attrstat *resp = rqstp->rq_resp;
  130. dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh));
  131. fh_copy(&resp->fh, &argp->fh);
  132. resp->status = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
  133. if (resp->status != nfs_ok)
  134. goto out;
  135. resp->status = fh_getattr(&resp->fh, &resp->stat);
  136. out:
  137. return rpc_success;
  138. }
  139. /*
  140. * Check file access
  141. */
  142. static __be32 nfsacld_proc_access(struct svc_rqst *rqstp)
  143. {
  144. struct nfsd3_accessargs *argp = rqstp->rq_argp;
  145. struct nfsd3_accessres *resp = rqstp->rq_resp;
  146. dprintk("nfsd: ACCESS(2acl) %s 0x%x\n",
  147. SVCFH_fmt(&argp->fh),
  148. argp->access);
  149. fh_copy(&resp->fh, &argp->fh);
  150. resp->access = argp->access;
  151. resp->status = nfsd_access(rqstp, &resp->fh, &resp->access, NULL);
  152. if (resp->status != nfs_ok)
  153. goto out;
  154. resp->status = fh_getattr(&resp->fh, &resp->stat);
  155. out:
  156. return rpc_success;
  157. }
  158. /*
  159. * XDR decode functions
  160. */
  161. static bool
  162. nfsaclsvc_decode_getaclargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  163. {
  164. struct nfsd3_getaclargs *argp = rqstp->rq_argp;
  165. if (!svcxdr_decode_fhandle(xdr, &argp->fh))
  166. return false;
  167. if (xdr_stream_decode_u32(xdr, &argp->mask) < 0)
  168. return false;
  169. return true;
  170. }
  171. static bool
  172. nfsaclsvc_decode_setaclargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  173. {
  174. struct nfsd3_setaclargs *argp = rqstp->rq_argp;
  175. if (!svcxdr_decode_fhandle(xdr, &argp->fh))
  176. return false;
  177. if (xdr_stream_decode_u32(xdr, &argp->mask) < 0)
  178. return false;
  179. if (argp->mask & ~NFS_ACL_MASK)
  180. return false;
  181. if (!nfs_stream_decode_acl(xdr, NULL, (argp->mask & NFS_ACL) ?
  182. &argp->acl_access : NULL))
  183. return false;
  184. if (!nfs_stream_decode_acl(xdr, NULL, (argp->mask & NFS_DFACL) ?
  185. &argp->acl_default : NULL))
  186. return false;
  187. return true;
  188. }
  189. static bool
  190. nfsaclsvc_decode_accessargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  191. {
  192. struct nfsd3_accessargs *args = rqstp->rq_argp;
  193. if (!svcxdr_decode_fhandle(xdr, &args->fh))
  194. return false;
  195. if (xdr_stream_decode_u32(xdr, &args->access) < 0)
  196. return false;
  197. return true;
  198. }
  199. /*
  200. * XDR encode functions
  201. */
  202. /* GETACL */
  203. static bool
  204. nfsaclsvc_encode_getaclres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  205. {
  206. struct nfsd3_getaclres *resp = rqstp->rq_resp;
  207. struct dentry *dentry = resp->fh.fh_dentry;
  208. struct inode *inode;
  209. if (!svcxdr_encode_stat(xdr, resp->status))
  210. return false;
  211. if (dentry == NULL || d_really_is_negative(dentry))
  212. return true;
  213. inode = d_inode(dentry);
  214. if (!svcxdr_encode_fattr(rqstp, xdr, &resp->fh, &resp->stat))
  215. return false;
  216. if (xdr_stream_encode_u32(xdr, resp->mask) < 0)
  217. return false;
  218. if (!nfs_stream_encode_acl(xdr, inode, resp->acl_access,
  219. resp->mask & NFS_ACL, 0))
  220. return false;
  221. if (!nfs_stream_encode_acl(xdr, inode, resp->acl_default,
  222. resp->mask & NFS_DFACL, NFS_ACL_DEFAULT))
  223. return false;
  224. return true;
  225. }
  226. /* ACCESS */
  227. static bool
  228. nfsaclsvc_encode_accessres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  229. {
  230. struct nfsd3_accessres *resp = rqstp->rq_resp;
  231. if (!svcxdr_encode_stat(xdr, resp->status))
  232. return false;
  233. switch (resp->status) {
  234. case nfs_ok:
  235. if (!svcxdr_encode_fattr(rqstp, xdr, &resp->fh, &resp->stat))
  236. return false;
  237. if (xdr_stream_encode_u32(xdr, resp->access) < 0)
  238. return false;
  239. break;
  240. }
  241. return true;
  242. }
  243. /*
  244. * XDR release functions
  245. */
  246. static void nfsaclsvc_release_getacl(struct svc_rqst *rqstp)
  247. {
  248. struct nfsd3_getaclres *resp = rqstp->rq_resp;
  249. fh_put(&resp->fh);
  250. posix_acl_release(resp->acl_access);
  251. posix_acl_release(resp->acl_default);
  252. }
  253. static void nfsaclsvc_release_access(struct svc_rqst *rqstp)
  254. {
  255. struct nfsd3_accessres *resp = rqstp->rq_resp;
  256. fh_put(&resp->fh);
  257. }
  258. #define ST 1 /* status*/
  259. #define AT 21 /* attributes */
  260. #define pAT (1+AT) /* post attributes - conditional */
  261. #define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
  262. static const struct svc_procedure nfsd_acl_procedures2[5] = {
  263. [ACLPROC2_NULL] = {
  264. .pc_func = nfsacld_proc_null,
  265. .pc_decode = nfssvc_decode_voidarg,
  266. .pc_encode = nfssvc_encode_voidres,
  267. .pc_argsize = sizeof(struct nfsd_voidargs),
  268. .pc_argzero = sizeof(struct nfsd_voidargs),
  269. .pc_ressize = sizeof(struct nfsd_voidres),
  270. .pc_cachetype = RC_NOCACHE,
  271. .pc_xdrressize = ST,
  272. .pc_name = "NULL",
  273. },
  274. [ACLPROC2_GETACL] = {
  275. .pc_func = nfsacld_proc_getacl,
  276. .pc_decode = nfsaclsvc_decode_getaclargs,
  277. .pc_encode = nfsaclsvc_encode_getaclres,
  278. .pc_release = nfsaclsvc_release_getacl,
  279. .pc_argsize = sizeof(struct nfsd3_getaclargs),
  280. .pc_argzero = sizeof(struct nfsd3_getaclargs),
  281. .pc_ressize = sizeof(struct nfsd3_getaclres),
  282. .pc_cachetype = RC_NOCACHE,
  283. .pc_xdrressize = ST+1+2*(1+ACL),
  284. .pc_name = "GETACL",
  285. },
  286. [ACLPROC2_SETACL] = {
  287. .pc_func = nfsacld_proc_setacl,
  288. .pc_decode = nfsaclsvc_decode_setaclargs,
  289. .pc_encode = nfssvc_encode_attrstatres,
  290. .pc_release = nfssvc_release_attrstat,
  291. .pc_argsize = sizeof(struct nfsd3_setaclargs),
  292. .pc_argzero = sizeof(struct nfsd3_setaclargs),
  293. .pc_ressize = sizeof(struct nfsd_attrstat),
  294. .pc_cachetype = RC_NOCACHE,
  295. .pc_xdrressize = ST+AT,
  296. .pc_name = "SETACL",
  297. },
  298. [ACLPROC2_GETATTR] = {
  299. .pc_func = nfsacld_proc_getattr,
  300. .pc_decode = nfssvc_decode_fhandleargs,
  301. .pc_encode = nfssvc_encode_attrstatres,
  302. .pc_release = nfssvc_release_attrstat,
  303. .pc_argsize = sizeof(struct nfsd_fhandle),
  304. .pc_argzero = sizeof(struct nfsd_fhandle),
  305. .pc_ressize = sizeof(struct nfsd_attrstat),
  306. .pc_cachetype = RC_NOCACHE,
  307. .pc_xdrressize = ST+AT,
  308. .pc_name = "GETATTR",
  309. },
  310. [ACLPROC2_ACCESS] = {
  311. .pc_func = nfsacld_proc_access,
  312. .pc_decode = nfsaclsvc_decode_accessargs,
  313. .pc_encode = nfsaclsvc_encode_accessres,
  314. .pc_release = nfsaclsvc_release_access,
  315. .pc_argsize = sizeof(struct nfsd3_accessargs),
  316. .pc_argzero = sizeof(struct nfsd3_accessargs),
  317. .pc_ressize = sizeof(struct nfsd3_accessres),
  318. .pc_cachetype = RC_NOCACHE,
  319. .pc_xdrressize = ST+AT+1,
  320. .pc_name = "SETATTR",
  321. },
  322. };
  323. static DEFINE_PER_CPU_ALIGNED(unsigned long,
  324. nfsd_acl_count2[ARRAY_SIZE(nfsd_acl_procedures2)]);
  325. const struct svc_version nfsd_acl_version2 = {
  326. .vs_vers = 2,
  327. .vs_nproc = ARRAY_SIZE(nfsd_acl_procedures2),
  328. .vs_proc = nfsd_acl_procedures2,
  329. .vs_count = nfsd_acl_count2,
  330. .vs_dispatch = nfsd_dispatch,
  331. .vs_xdrsize = NFS3_SVC_XDRSIZE,
  332. };