nfs2acl.c 8.6 KB

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