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 "cache.h"
  10. #include "xdr3.h"
  11. #include "vfs.h"
  12. #define NFSDDBG_FACILITY NFSDDBG_PROC
  13. #define RETURN_STATUS(st) { resp->status = (st); return (st); }
  14. /*
  15. * NULL call.
  16. */
  17. static __be32
  18. nfsacld_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
  19. {
  20. return nfs_ok;
  21. }
  22. /*
  23. * Get the Access and/or Default ACL of a file.
  24. */
  25. static __be32 nfsacld_proc_getacl(struct svc_rqst * rqstp,
  26. struct nfsd3_getaclargs *argp, struct nfsd3_getaclres *resp)
  27. {
  28. svc_fh *fh;
  29. struct posix_acl *acl;
  30. __be32 nfserr = 0;
  31. dprintk("nfsd: GETACL(2acl) %s\n", SVCFH_fmt(&argp->fh));
  32. fh = fh_copy(&resp->fh, &argp->fh);
  33. nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
  34. if (nfserr)
  35. RETURN_STATUS(nfserr);
  36. if (argp->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT))
  37. RETURN_STATUS(nfserr_inval);
  38. resp->mask = argp->mask;
  39. if (resp->mask & (NFS_ACL|NFS_ACLCNT)) {
  40. acl = nfsd_get_posix_acl(fh, ACL_TYPE_ACCESS);
  41. if (IS_ERR(acl)) {
  42. int err = PTR_ERR(acl);
  43. if (err == -ENODATA || err == -EOPNOTSUPP)
  44. acl = NULL;
  45. else {
  46. nfserr = nfserrno(err);
  47. goto fail;
  48. }
  49. }
  50. if (acl == NULL) {
  51. /* Solaris returns the inode's minimum ACL. */
  52. struct inode *inode = fh->fh_dentry->d_inode;
  53. acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  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 = nfsd_get_posix_acl(fh, ACL_TYPE_DEFAULT);
  61. if (IS_ERR(acl)) {
  62. int err = PTR_ERR(acl);
  63. if (err == -ENODATA || err == -EOPNOTSUPP)
  64. acl = NULL;
  65. else {
  66. nfserr = nfserrno(err);
  67. goto fail;
  68. }
  69. }
  70. resp->acl_default = acl;
  71. }
  72. /* resp->acl_{access,default} are released in nfssvc_release_getacl. */
  73. RETURN_STATUS(0);
  74. fail:
  75. posix_acl_release(resp->acl_access);
  76. posix_acl_release(resp->acl_default);
  77. RETURN_STATUS(nfserr);
  78. }
  79. /*
  80. * Set the Access and/or Default ACL of a file.
  81. */
  82. static __be32 nfsacld_proc_setacl(struct svc_rqst * rqstp,
  83. struct nfsd3_setaclargs *argp,
  84. struct nfsd_attrstat *resp)
  85. {
  86. svc_fh *fh;
  87. __be32 nfserr = 0;
  88. dprintk("nfsd: SETACL(2acl) %s\n", SVCFH_fmt(&argp->fh));
  89. fh = fh_copy(&resp->fh, &argp->fh);
  90. nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
  91. if (!nfserr) {
  92. nfserr = nfserrno( nfsd_set_posix_acl(
  93. fh, ACL_TYPE_ACCESS, argp->acl_access) );
  94. }
  95. if (!nfserr) {
  96. nfserr = nfserrno( nfsd_set_posix_acl(
  97. fh, ACL_TYPE_DEFAULT, argp->acl_default) );
  98. }
  99. /* argp->acl_{access,default} may have been allocated in
  100. nfssvc_decode_setaclargs. */
  101. posix_acl_release(argp->acl_access);
  102. posix_acl_release(argp->acl_default);
  103. return nfserr;
  104. }
  105. /*
  106. * Check file attributes
  107. */
  108. static __be32 nfsacld_proc_getattr(struct svc_rqst * rqstp,
  109. struct nfsd_fhandle *argp, struct nfsd_attrstat *resp)
  110. {
  111. dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh));
  112. fh_copy(&resp->fh, &argp->fh);
  113. return fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
  114. }
  115. /*
  116. * Check file access
  117. */
  118. static __be32 nfsacld_proc_access(struct svc_rqst *rqstp, struct nfsd3_accessargs *argp,
  119. struct nfsd3_accessres *resp)
  120. {
  121. __be32 nfserr;
  122. dprintk("nfsd: ACCESS(2acl) %s 0x%x\n",
  123. SVCFH_fmt(&argp->fh),
  124. argp->access);
  125. fh_copy(&resp->fh, &argp->fh);
  126. resp->access = argp->access;
  127. nfserr = nfsd_access(rqstp, &resp->fh, &resp->access, NULL);
  128. return nfserr;
  129. }
  130. /*
  131. * XDR decode functions
  132. */
  133. static int nfsaclsvc_decode_getaclargs(struct svc_rqst *rqstp, __be32 *p,
  134. struct nfsd3_getaclargs *argp)
  135. {
  136. if (!(p = nfs2svc_decode_fh(p, &argp->fh)))
  137. return 0;
  138. argp->mask = ntohl(*p); p++;
  139. return xdr_argsize_check(rqstp, p);
  140. }
  141. static int nfsaclsvc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p,
  142. struct nfsd3_setaclargs *argp)
  143. {
  144. struct kvec *head = rqstp->rq_arg.head;
  145. unsigned int base;
  146. int n;
  147. if (!(p = nfs2svc_decode_fh(p, &argp->fh)))
  148. return 0;
  149. argp->mask = ntohl(*p++);
  150. if (argp->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT) ||
  151. !xdr_argsize_check(rqstp, p))
  152. return 0;
  153. base = (char *)p - (char *)head->iov_base;
  154. n = nfsacl_decode(&rqstp->rq_arg, base, NULL,
  155. (argp->mask & NFS_ACL) ?
  156. &argp->acl_access : NULL);
  157. if (n > 0)
  158. n = nfsacl_decode(&rqstp->rq_arg, base + n, NULL,
  159. (argp->mask & NFS_DFACL) ?
  160. &argp->acl_default : NULL);
  161. return (n > 0);
  162. }
  163. static int nfsaclsvc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p,
  164. struct nfsd_fhandle *argp)
  165. {
  166. if (!(p = nfs2svc_decode_fh(p, &argp->fh)))
  167. return 0;
  168. return xdr_argsize_check(rqstp, p);
  169. }
  170. static int nfsaclsvc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
  171. struct nfsd3_accessargs *argp)
  172. {
  173. if (!(p = nfs2svc_decode_fh(p, &argp->fh)))
  174. return 0;
  175. argp->access = ntohl(*p++);
  176. return xdr_argsize_check(rqstp, p);
  177. }
  178. /*
  179. * XDR encode functions
  180. */
  181. /*
  182. * There must be an encoding function for void results so svc_process
  183. * will work properly.
  184. */
  185. int
  186. 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. };