nfs2acl.c 9.2 KB

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