nfs2acl.c 9.3 KB

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