nfs2acl.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * linux/fs/nfsd/nfs2acl.c
  3. *
  4. * Process version 2 NFSACL requests.
  5. *
  6. * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
  7. */
  8. #include <linux/sunrpc/svc.h>
  9. #include <linux/nfs.h>
  10. #include <linux/nfsd/nfsd.h>
  11. #include <linux/nfsd/cache.h>
  12. #include <linux/nfsd/xdr.h>
  13. #include <linux/nfsd/xdr3.h>
  14. #include <linux/posix_acl.h>
  15. #include <linux/nfsacl.h>
  16. #define NFSDDBG_FACILITY NFSDDBG_PROC
  17. #define RETURN_STATUS(st) { resp->status = (st); return (st); }
  18. /*
  19. * NULL call.
  20. */
  21. static __be32
  22. nfsacld_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
  23. {
  24. return nfs_ok;
  25. }
  26. /*
  27. * Get the Access and/or Default ACL of a file.
  28. */
  29. static __be32 nfsacld_proc_getacl(struct svc_rqst * rqstp,
  30. struct nfsd3_getaclargs *argp, struct nfsd3_getaclres *resp)
  31. {
  32. svc_fh *fh;
  33. struct posix_acl *acl;
  34. __be32 nfserr = 0;
  35. dprintk("nfsd: GETACL(2acl) %s\n", SVCFH_fmt(&argp->fh));
  36. fh = fh_copy(&resp->fh, &argp->fh);
  37. nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
  38. if (nfserr)
  39. RETURN_STATUS(nfserr);
  40. if (argp->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT))
  41. RETURN_STATUS(nfserr_inval);
  42. resp->mask = argp->mask;
  43. if (resp->mask & (NFS_ACL|NFS_ACLCNT)) {
  44. acl = nfsd_get_posix_acl(fh, ACL_TYPE_ACCESS);
  45. if (IS_ERR(acl)) {
  46. int err = PTR_ERR(acl);
  47. if (err == -ENODATA || err == -EOPNOTSUPP)
  48. acl = NULL;
  49. else {
  50. nfserr = nfserrno(err);
  51. goto fail;
  52. }
  53. }
  54. if (acl == NULL) {
  55. /* Solaris returns the inode's minimum ACL. */
  56. struct inode *inode = fh->fh_dentry->d_inode;
  57. acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  58. }
  59. resp->acl_access = acl;
  60. }
  61. if (resp->mask & (NFS_DFACL|NFS_DFACLCNT)) {
  62. /* Check how Solaris handles requests for the Default ACL
  63. of a non-directory! */
  64. acl = nfsd_get_posix_acl(fh, ACL_TYPE_DEFAULT);
  65. if (IS_ERR(acl)) {
  66. int err = PTR_ERR(acl);
  67. if (err == -ENODATA || err == -EOPNOTSUPP)
  68. acl = NULL;
  69. else {
  70. nfserr = nfserrno(err);
  71. goto fail;
  72. }
  73. }
  74. resp->acl_default = acl;
  75. }
  76. /* resp->acl_{access,default} are released in nfssvc_release_getacl. */
  77. RETURN_STATUS(0);
  78. fail:
  79. posix_acl_release(resp->acl_access);
  80. posix_acl_release(resp->acl_default);
  81. RETURN_STATUS(nfserr);
  82. }
  83. /*
  84. * Set the Access and/or Default ACL of a file.
  85. */
  86. static __be32 nfsacld_proc_setacl(struct svc_rqst * rqstp,
  87. struct nfsd3_setaclargs *argp,
  88. struct nfsd_attrstat *resp)
  89. {
  90. svc_fh *fh;
  91. __be32 nfserr = 0;
  92. dprintk("nfsd: SETACL(2acl) %s\n", SVCFH_fmt(&argp->fh));
  93. fh = fh_copy(&resp->fh, &argp->fh);
  94. nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
  95. if (!nfserr) {
  96. nfserr = nfserrno( nfsd_set_posix_acl(
  97. fh, ACL_TYPE_ACCESS, argp->acl_access) );
  98. }
  99. if (!nfserr) {
  100. nfserr = nfserrno( nfsd_set_posix_acl(
  101. fh, ACL_TYPE_DEFAULT, argp->acl_default) );
  102. }
  103. /* argp->acl_{access,default} may have been allocated in
  104. nfssvc_decode_setaclargs. */
  105. posix_acl_release(argp->acl_access);
  106. posix_acl_release(argp->acl_default);
  107. return nfserr;
  108. }
  109. /*
  110. * Check file attributes
  111. */
  112. static __be32 nfsacld_proc_getattr(struct svc_rqst * rqstp,
  113. struct nfsd_fhandle *argp, struct nfsd_attrstat *resp)
  114. {
  115. dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh));
  116. fh_copy(&resp->fh, &argp->fh);
  117. return fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
  118. }
  119. /*
  120. * Check file access
  121. */
  122. static __be32 nfsacld_proc_access(struct svc_rqst *rqstp, struct nfsd3_accessargs *argp,
  123. struct nfsd3_accessres *resp)
  124. {
  125. __be32 nfserr;
  126. dprintk("nfsd: ACCESS(2acl) %s 0x%x\n",
  127. SVCFH_fmt(&argp->fh),
  128. argp->access);
  129. fh_copy(&resp->fh, &argp->fh);
  130. resp->access = argp->access;
  131. nfserr = nfsd_access(rqstp, &resp->fh, &resp->access, NULL);
  132. return nfserr;
  133. }
  134. /*
  135. * XDR decode functions
  136. */
  137. static int nfsaclsvc_decode_getaclargs(struct svc_rqst *rqstp, __be32 *p,
  138. struct nfsd3_getaclargs *argp)
  139. {
  140. if (!(p = nfs2svc_decode_fh(p, &argp->fh)))
  141. return 0;
  142. argp->mask = ntohl(*p); p++;
  143. return xdr_argsize_check(rqstp, p);
  144. }
  145. static int nfsaclsvc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p,
  146. struct nfsd3_setaclargs *argp)
  147. {
  148. struct kvec *head = rqstp->rq_arg.head;
  149. unsigned int base;
  150. int n;
  151. if (!(p = nfs2svc_decode_fh(p, &argp->fh)))
  152. return 0;
  153. argp->mask = ntohl(*p++);
  154. if (argp->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT) ||
  155. !xdr_argsize_check(rqstp, p))
  156. return 0;
  157. base = (char *)p - (char *)head->iov_base;
  158. n = nfsacl_decode(&rqstp->rq_arg, base, NULL,
  159. (argp->mask & NFS_ACL) ?
  160. &argp->acl_access : NULL);
  161. if (n > 0)
  162. n = nfsacl_decode(&rqstp->rq_arg, base + n, NULL,
  163. (argp->mask & NFS_DFACL) ?
  164. &argp->acl_default : NULL);
  165. return (n > 0);
  166. }
  167. static int nfsaclsvc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p,
  168. struct nfsd_fhandle *argp)
  169. {
  170. if (!(p = nfs2svc_decode_fh(p, &argp->fh)))
  171. return 0;
  172. return xdr_argsize_check(rqstp, p);
  173. }
  174. static int nfsaclsvc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
  175. struct nfsd3_accessargs *argp)
  176. {
  177. if (!(p = nfs2svc_decode_fh(p, &argp->fh)))
  178. return 0;
  179. argp->access = ntohl(*p++);
  180. return xdr_argsize_check(rqstp, p);
  181. }
  182. /*
  183. * XDR encode functions
  184. */
  185. /* GETACL */
  186. static int nfsaclsvc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p,
  187. struct nfsd3_getaclres *resp)
  188. {
  189. struct dentry *dentry = resp->fh.fh_dentry;
  190. struct inode *inode;
  191. struct kvec *head = rqstp->rq_res.head;
  192. unsigned int base;
  193. int n;
  194. int w;
  195. /*
  196. * Since this is version 2, the check for nfserr in
  197. * nfsd_dispatch actually ensures the following cannot happen.
  198. * However, it seems fragile to depend on that.
  199. */
  200. if (dentry == NULL || dentry->d_inode == NULL)
  201. return 0;
  202. inode = dentry->d_inode;
  203. p = nfs2svc_encode_fattr(rqstp, p, &resp->fh);
  204. *p++ = htonl(resp->mask);
  205. if (!xdr_ressize_check(rqstp, p))
  206. return 0;
  207. base = (char *)p - (char *)head->iov_base;
  208. rqstp->rq_res.page_len = w = nfsacl_size(
  209. (resp->mask & NFS_ACL) ? resp->acl_access : NULL,
  210. (resp->mask & NFS_DFACL) ? resp->acl_default : NULL);
  211. while (w > 0) {
  212. if (!rqstp->rq_respages[rqstp->rq_resused++])
  213. return 0;
  214. w -= PAGE_SIZE;
  215. }
  216. n = nfsacl_encode(&rqstp->rq_res, base, inode,
  217. resp->acl_access,
  218. resp->mask & NFS_ACL, 0);
  219. if (n > 0)
  220. n = nfsacl_encode(&rqstp->rq_res, base + n, inode,
  221. resp->acl_default,
  222. resp->mask & NFS_DFACL,
  223. NFS_ACL_DEFAULT);
  224. if (n <= 0)
  225. return 0;
  226. return 1;
  227. }
  228. static int nfsaclsvc_encode_attrstatres(struct svc_rqst *rqstp, __be32 *p,
  229. struct nfsd_attrstat *resp)
  230. {
  231. p = nfs2svc_encode_fattr(rqstp, p, &resp->fh);
  232. return xdr_ressize_check(rqstp, p);
  233. }
  234. /* ACCESS */
  235. static int nfsaclsvc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
  236. struct nfsd3_accessres *resp)
  237. {
  238. p = nfs2svc_encode_fattr(rqstp, p, &resp->fh);
  239. *p++ = htonl(resp->access);
  240. return xdr_ressize_check(rqstp, p);
  241. }
  242. /*
  243. * XDR release functions
  244. */
  245. static int nfsaclsvc_release_getacl(struct svc_rqst *rqstp, __be32 *p,
  246. struct nfsd3_getaclres *resp)
  247. {
  248. fh_put(&resp->fh);
  249. posix_acl_release(resp->acl_access);
  250. posix_acl_release(resp->acl_default);
  251. return 1;
  252. }
  253. static int nfsaclsvc_release_attrstat(struct svc_rqst *rqstp, __be32 *p,
  254. struct nfsd_attrstat *resp)
  255. {
  256. fh_put(&resp->fh);
  257. return 1;
  258. }
  259. static int nfsaclsvc_release_access(struct svc_rqst *rqstp, __be32 *p,
  260. struct nfsd3_accessres *resp)
  261. {
  262. fh_put(&resp->fh);
  263. return 1;
  264. }
  265. #define nfsaclsvc_decode_voidargs NULL
  266. #define nfsaclsvc_encode_voidres NULL
  267. #define nfsaclsvc_release_void NULL
  268. #define nfsd3_fhandleargs nfsd_fhandle
  269. #define nfsd3_attrstatres nfsd_attrstat
  270. #define nfsd3_voidres nfsd3_voidargs
  271. struct nfsd3_voidargs { int dummy; };
  272. #define PROC(name, argt, rest, relt, cache, respsize) \
  273. { (svc_procfunc) nfsacld_proc_##name, \
  274. (kxdrproc_t) nfsaclsvc_decode_##argt##args, \
  275. (kxdrproc_t) nfsaclsvc_encode_##rest##res, \
  276. (kxdrproc_t) nfsaclsvc_release_##relt, \
  277. sizeof(struct nfsd3_##argt##args), \
  278. sizeof(struct nfsd3_##rest##res), \
  279. 0, \
  280. cache, \
  281. respsize, \
  282. }
  283. #define ST 1 /* status*/
  284. #define AT 21 /* attributes */
  285. #define pAT (1+AT) /* post attributes - conditional */
  286. #define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
  287. static struct svc_procedure nfsd_acl_procedures2[] = {
  288. PROC(null, void, void, void, RC_NOCACHE, ST),
  289. PROC(getacl, getacl, getacl, getacl, RC_NOCACHE, ST+1+2*(1+ACL)),
  290. PROC(setacl, setacl, attrstat, attrstat, RC_NOCACHE, ST+AT),
  291. PROC(getattr, fhandle, attrstat, attrstat, RC_NOCACHE, ST+AT),
  292. PROC(access, access, access, access, RC_NOCACHE, ST+AT+1),
  293. };
  294. struct svc_version nfsd_acl_version2 = {
  295. .vs_vers = 2,
  296. .vs_nproc = 5,
  297. .vs_proc = nfsd_acl_procedures2,
  298. .vs_dispatch = nfsd_dispatch,
  299. .vs_xdrsize = NFS3_SVC_XDRSIZE,
  300. .vs_hidden = 1,
  301. };