nfs2acl.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. static int nfsaclsvc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  187. {
  188. return xdr_ressize_check(rqstp, p);
  189. }
  190. /* GETACL */
  191. static int nfsaclsvc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p,
  192. struct nfsd3_getaclres *resp)
  193. {
  194. struct dentry *dentry = resp->fh.fh_dentry;
  195. struct inode *inode;
  196. struct kvec *head = rqstp->rq_res.head;
  197. unsigned int base;
  198. int n;
  199. int w;
  200. /*
  201. * Since this is version 2, the check for nfserr in
  202. * nfsd_dispatch actually ensures the following cannot happen.
  203. * However, it seems fragile to depend on that.
  204. */
  205. if (dentry == NULL || dentry->d_inode == NULL)
  206. return 0;
  207. inode = dentry->d_inode;
  208. p = nfs2svc_encode_fattr(rqstp, p, &resp->fh);
  209. *p++ = htonl(resp->mask);
  210. if (!xdr_ressize_check(rqstp, p))
  211. return 0;
  212. base = (char *)p - (char *)head->iov_base;
  213. rqstp->rq_res.page_len = w = nfsacl_size(
  214. (resp->mask & NFS_ACL) ? resp->acl_access : NULL,
  215. (resp->mask & NFS_DFACL) ? resp->acl_default : NULL);
  216. while (w > 0) {
  217. if (!rqstp->rq_respages[rqstp->rq_resused++])
  218. return 0;
  219. w -= PAGE_SIZE;
  220. }
  221. n = nfsacl_encode(&rqstp->rq_res, base, inode,
  222. resp->acl_access,
  223. resp->mask & NFS_ACL, 0);
  224. if (n > 0)
  225. n = nfsacl_encode(&rqstp->rq_res, base + n, inode,
  226. resp->acl_default,
  227. resp->mask & NFS_DFACL,
  228. NFS_ACL_DEFAULT);
  229. if (n <= 0)
  230. return 0;
  231. return 1;
  232. }
  233. static int nfsaclsvc_encode_attrstatres(struct svc_rqst *rqstp, __be32 *p,
  234. struct nfsd_attrstat *resp)
  235. {
  236. p = nfs2svc_encode_fattr(rqstp, p, &resp->fh);
  237. return xdr_ressize_check(rqstp, p);
  238. }
  239. /* ACCESS */
  240. static int nfsaclsvc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
  241. struct nfsd3_accessres *resp)
  242. {
  243. p = nfs2svc_encode_fattr(rqstp, p, &resp->fh);
  244. *p++ = htonl(resp->access);
  245. return xdr_ressize_check(rqstp, p);
  246. }
  247. /*
  248. * XDR release functions
  249. */
  250. static int nfsaclsvc_release_getacl(struct svc_rqst *rqstp, __be32 *p,
  251. struct nfsd3_getaclres *resp)
  252. {
  253. fh_put(&resp->fh);
  254. posix_acl_release(resp->acl_access);
  255. posix_acl_release(resp->acl_default);
  256. return 1;
  257. }
  258. static int nfsaclsvc_release_attrstat(struct svc_rqst *rqstp, __be32 *p,
  259. struct nfsd_attrstat *resp)
  260. {
  261. fh_put(&resp->fh);
  262. return 1;
  263. }
  264. static int nfsaclsvc_release_access(struct svc_rqst *rqstp, __be32 *p,
  265. struct nfsd3_accessres *resp)
  266. {
  267. fh_put(&resp->fh);
  268. return 1;
  269. }
  270. #define nfsaclsvc_decode_voidargs NULL
  271. #define nfsaclsvc_release_void NULL
  272. #define nfsd3_fhandleargs nfsd_fhandle
  273. #define nfsd3_attrstatres nfsd_attrstat
  274. #define nfsd3_voidres nfsd3_voidargs
  275. struct nfsd3_voidargs { int dummy; };
  276. #define PROC(name, argt, rest, relt, cache, respsize) \
  277. { (svc_procfunc) nfsacld_proc_##name, \
  278. (kxdrproc_t) nfsaclsvc_decode_##argt##args, \
  279. (kxdrproc_t) nfsaclsvc_encode_##rest##res, \
  280. (kxdrproc_t) nfsaclsvc_release_##relt, \
  281. sizeof(struct nfsd3_##argt##args), \
  282. sizeof(struct nfsd3_##rest##res), \
  283. 0, \
  284. cache, \
  285. respsize, \
  286. }
  287. #define ST 1 /* status*/
  288. #define AT 21 /* attributes */
  289. #define pAT (1+AT) /* post attributes - conditional */
  290. #define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
  291. static struct svc_procedure nfsd_acl_procedures2[] = {
  292. PROC(null, void, void, void, RC_NOCACHE, ST),
  293. PROC(getacl, getacl, getacl, getacl, RC_NOCACHE, ST+1+2*(1+ACL)),
  294. PROC(setacl, setacl, attrstat, attrstat, RC_NOCACHE, ST+AT),
  295. PROC(getattr, fhandle, attrstat, attrstat, RC_NOCACHE, ST+AT),
  296. PROC(access, access, access, access, RC_NOCACHE, ST+AT+1),
  297. };
  298. struct svc_version nfsd_acl_version2 = {
  299. .vs_vers = 2,
  300. .vs_nproc = 5,
  301. .vs_proc = nfsd_acl_procedures2,
  302. .vs_dispatch = nfsd_dispatch,
  303. .vs_xdrsize = NFS3_SVC_XDRSIZE,
  304. .vs_hidden = 0,
  305. };