nfs2acl.c 9.1 KB

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