Loading...
1/*
2 * Process version 3 NFSACL requests.
3 *
4 * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
5 */
6
7#include "nfsd.h"
8/* FIXME: nfsacl.h is a broken header */
9#include <linux/nfsacl.h>
10#include <linux/gfp.h>
11#include "cache.h"
12#include "xdr3.h"
13#include "vfs.h"
14
15#define RETURN_STATUS(st) { resp->status = (st); return (st); }
16
17/*
18 * NULL call.
19 */
20static __be32
21nfsd3_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
22{
23 return nfs_ok;
24}
25
26/*
27 * Get the Access and/or Default ACL of a file.
28 */
29static __be32 nfsd3_proc_getacl(struct svc_rqst * rqstp,
30 struct nfsd3_getaclargs *argp, struct nfsd3_getaclres *resp)
31{
32 svc_fh *fh;
33 struct posix_acl *acl;
34 __be32 nfserr = 0;
35
36 fh = fh_copy(&resp->fh, &argp->fh);
37 nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
38 if (nfserr)
39 RETURN_STATUS(nfserr);
40
41 if (argp->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT))
42 RETURN_STATUS(nfserr_inval);
43 resp->mask = argp->mask;
44
45 if (resp->mask & (NFS_ACL|NFS_ACLCNT)) {
46 acl = nfsd_get_posix_acl(fh, ACL_TYPE_ACCESS);
47 if (IS_ERR(acl)) {
48 int err = PTR_ERR(acl);
49
50 if (err == -ENODATA || err == -EOPNOTSUPP)
51 acl = NULL;
52 else {
53 nfserr = nfserrno(err);
54 goto fail;
55 }
56 }
57 if (acl == NULL) {
58 /* Solaris returns the inode's minimum ACL. */
59
60 struct inode *inode = fh->fh_dentry->d_inode;
61 acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
62 }
63 resp->acl_access = acl;
64 }
65 if (resp->mask & (NFS_DFACL|NFS_DFACLCNT)) {
66 /* Check how Solaris handles requests for the Default ACL
67 of a non-directory! */
68
69 acl = nfsd_get_posix_acl(fh, ACL_TYPE_DEFAULT);
70 if (IS_ERR(acl)) {
71 int err = PTR_ERR(acl);
72
73 if (err == -ENODATA || err == -EOPNOTSUPP)
74 acl = NULL;
75 else {
76 nfserr = nfserrno(err);
77 goto fail;
78 }
79 }
80 resp->acl_default = acl;
81 }
82
83 /* resp->acl_{access,default} are released in nfs3svc_release_getacl. */
84 RETURN_STATUS(0);
85
86fail:
87 posix_acl_release(resp->acl_access);
88 posix_acl_release(resp->acl_default);
89 RETURN_STATUS(nfserr);
90}
91
92/*
93 * Set the Access and/or Default ACL of a file.
94 */
95static __be32 nfsd3_proc_setacl(struct svc_rqst * rqstp,
96 struct nfsd3_setaclargs *argp,
97 struct nfsd3_attrstat *resp)
98{
99 svc_fh *fh;
100 __be32 nfserr = 0;
101
102 fh = fh_copy(&resp->fh, &argp->fh);
103 nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
104
105 if (!nfserr) {
106 nfserr = nfserrno( nfsd_set_posix_acl(
107 fh, ACL_TYPE_ACCESS, argp->acl_access) );
108 }
109 if (!nfserr) {
110 nfserr = nfserrno( nfsd_set_posix_acl(
111 fh, ACL_TYPE_DEFAULT, argp->acl_default) );
112 }
113
114 /* argp->acl_{access,default} may have been allocated in
115 nfs3svc_decode_setaclargs. */
116 posix_acl_release(argp->acl_access);
117 posix_acl_release(argp->acl_default);
118 RETURN_STATUS(nfserr);
119}
120
121/*
122 * XDR decode functions
123 */
124static int nfs3svc_decode_getaclargs(struct svc_rqst *rqstp, __be32 *p,
125 struct nfsd3_getaclargs *args)
126{
127 if (!(p = nfs3svc_decode_fh(p, &args->fh)))
128 return 0;
129 args->mask = ntohl(*p); p++;
130
131 return xdr_argsize_check(rqstp, p);
132}
133
134
135static int nfs3svc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p,
136 struct nfsd3_setaclargs *args)
137{
138 struct kvec *head = rqstp->rq_arg.head;
139 unsigned int base;
140 int n;
141
142 if (!(p = nfs3svc_decode_fh(p, &args->fh)))
143 return 0;
144 args->mask = ntohl(*p++);
145 if (args->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT) ||
146 !xdr_argsize_check(rqstp, p))
147 return 0;
148
149 base = (char *)p - (char *)head->iov_base;
150 n = nfsacl_decode(&rqstp->rq_arg, base, NULL,
151 (args->mask & NFS_ACL) ?
152 &args->acl_access : NULL);
153 if (n > 0)
154 n = nfsacl_decode(&rqstp->rq_arg, base + n, NULL,
155 (args->mask & NFS_DFACL) ?
156 &args->acl_default : NULL);
157 return (n > 0);
158}
159
160/*
161 * XDR encode functions
162 */
163
164/* GETACL */
165static int nfs3svc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p,
166 struct nfsd3_getaclres *resp)
167{
168 struct dentry *dentry = resp->fh.fh_dentry;
169
170 p = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh);
171 if (resp->status == 0 && dentry && dentry->d_inode) {
172 struct inode *inode = dentry->d_inode;
173 struct kvec *head = rqstp->rq_res.head;
174 unsigned int base;
175 int n;
176 int w;
177
178 *p++ = htonl(resp->mask);
179 if (!xdr_ressize_check(rqstp, p))
180 return 0;
181 base = (char *)p - (char *)head->iov_base;
182
183 rqstp->rq_res.page_len = w = nfsacl_size(
184 (resp->mask & NFS_ACL) ? resp->acl_access : NULL,
185 (resp->mask & NFS_DFACL) ? resp->acl_default : NULL);
186 while (w > 0) {
187 if (!rqstp->rq_respages[rqstp->rq_resused++])
188 return 0;
189 w -= PAGE_SIZE;
190 }
191
192 n = nfsacl_encode(&rqstp->rq_res, base, inode,
193 resp->acl_access,
194 resp->mask & NFS_ACL, 0);
195 if (n > 0)
196 n = nfsacl_encode(&rqstp->rq_res, base + n, inode,
197 resp->acl_default,
198 resp->mask & NFS_DFACL,
199 NFS_ACL_DEFAULT);
200 if (n <= 0)
201 return 0;
202 } else
203 if (!xdr_ressize_check(rqstp, p))
204 return 0;
205
206 return 1;
207}
208
209/* SETACL */
210static int nfs3svc_encode_setaclres(struct svc_rqst *rqstp, __be32 *p,
211 struct nfsd3_attrstat *resp)
212{
213 p = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh);
214
215 return xdr_ressize_check(rqstp, p);
216}
217
218/*
219 * XDR release functions
220 */
221static int nfs3svc_release_getacl(struct svc_rqst *rqstp, __be32 *p,
222 struct nfsd3_getaclres *resp)
223{
224 fh_put(&resp->fh);
225 posix_acl_release(resp->acl_access);
226 posix_acl_release(resp->acl_default);
227 return 1;
228}
229
230#define nfs3svc_decode_voidargs NULL
231#define nfs3svc_release_void NULL
232#define nfsd3_setaclres nfsd3_attrstat
233#define nfsd3_voidres nfsd3_voidargs
234struct nfsd3_voidargs { int dummy; };
235
236#define PROC(name, argt, rest, relt, cache, respsize) \
237 { (svc_procfunc) nfsd3_proc_##name, \
238 (kxdrproc_t) nfs3svc_decode_##argt##args, \
239 (kxdrproc_t) nfs3svc_encode_##rest##res, \
240 (kxdrproc_t) nfs3svc_release_##relt, \
241 sizeof(struct nfsd3_##argt##args), \
242 sizeof(struct nfsd3_##rest##res), \
243 0, \
244 cache, \
245 respsize, \
246 }
247
248#define ST 1 /* status*/
249#define AT 21 /* attributes */
250#define pAT (1+AT) /* post attributes - conditional */
251#define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
252
253static struct svc_procedure nfsd_acl_procedures3[] = {
254 PROC(null, void, void, void, RC_NOCACHE, ST),
255 PROC(getacl, getacl, getacl, getacl, RC_NOCACHE, ST+1+2*(1+ACL)),
256 PROC(setacl, setacl, setacl, fhandle, RC_NOCACHE, ST+pAT),
257};
258
259struct svc_version nfsd_acl_version3 = {
260 .vs_vers = 3,
261 .vs_nproc = 3,
262 .vs_proc = nfsd_acl_procedures3,
263 .vs_dispatch = nfsd_dispatch,
264 .vs_xdrsize = NFS3_SVC_XDRSIZE,
265 .vs_hidden = 0,
266};
267
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Process version 3 NFSACL requests.
4 *
5 * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
6 */
7
8#include "nfsd.h"
9/* FIXME: nfsacl.h is a broken header */
10#include <linux/nfsacl.h>
11#include <linux/gfp.h>
12#include "cache.h"
13#include "xdr3.h"
14#include "vfs.h"
15
16/*
17 * NULL call.
18 */
19static __be32
20nfsd3_proc_null(struct svc_rqst *rqstp)
21{
22 return rpc_success;
23}
24
25/*
26 * Get the Access and/or Default ACL of a file.
27 */
28static __be32 nfsd3_proc_getacl(struct svc_rqst *rqstp)
29{
30 struct nfsd3_getaclargs *argp = rqstp->rq_argp;
31 struct nfsd3_getaclres *resp = rqstp->rq_resp;
32 struct posix_acl *acl;
33 struct inode *inode;
34 svc_fh *fh;
35
36 fh = fh_copy(&resp->fh, &argp->fh);
37 resp->status = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
38 if (resp->status != nfs_ok)
39 goto out;
40
41 inode = d_inode(fh->fh_dentry);
42
43 if (argp->mask & ~NFS_ACL_MASK) {
44 resp->status = nfserr_inval;
45 goto out;
46 }
47 resp->mask = argp->mask;
48
49 if (resp->mask & (NFS_ACL|NFS_ACLCNT)) {
50 acl = get_inode_acl(inode, ACL_TYPE_ACCESS);
51 if (acl == NULL) {
52 /* Solaris returns the inode's minimum ACL. */
53 acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
54 }
55 if (IS_ERR(acl)) {
56 resp->status = nfserrno(PTR_ERR(acl));
57 goto fail;
58 }
59 resp->acl_access = acl;
60 }
61 if (resp->mask & (NFS_DFACL|NFS_DFACLCNT)) {
62 /* Check how Solaris handles requests for the Default ACL
63 of a non-directory! */
64 acl = get_inode_acl(inode, ACL_TYPE_DEFAULT);
65 if (IS_ERR(acl)) {
66 resp->status = nfserrno(PTR_ERR(acl));
67 goto fail;
68 }
69 resp->acl_default = acl;
70 }
71
72 /* resp->acl_{access,default} are released in nfs3svc_release_getacl. */
73out:
74 return rpc_success;
75
76fail:
77 posix_acl_release(resp->acl_access);
78 posix_acl_release(resp->acl_default);
79 goto out;
80}
81
82/*
83 * Set the Access and/or Default ACL of a file.
84 */
85static __be32 nfsd3_proc_setacl(struct svc_rqst *rqstp)
86{
87 struct nfsd3_setaclargs *argp = rqstp->rq_argp;
88 struct nfsd3_attrstat *resp = rqstp->rq_resp;
89 struct inode *inode;
90 svc_fh *fh;
91 int error;
92
93 fh = fh_copy(&resp->fh, &argp->fh);
94 resp->status = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
95 if (resp->status != nfs_ok)
96 goto out;
97
98 inode = d_inode(fh->fh_dentry);
99
100 error = fh_want_write(fh);
101 if (error)
102 goto out_errno;
103
104 inode_lock(inode);
105
106 error = set_posix_acl(&nop_mnt_idmap, fh->fh_dentry, ACL_TYPE_ACCESS,
107 argp->acl_access);
108 if (error)
109 goto out_drop_lock;
110 error = set_posix_acl(&nop_mnt_idmap, fh->fh_dentry, ACL_TYPE_DEFAULT,
111 argp->acl_default);
112
113out_drop_lock:
114 inode_unlock(inode);
115 fh_drop_write(fh);
116out_errno:
117 resp->status = nfserrno(error);
118out:
119 /* argp->acl_{access,default} may have been allocated in
120 nfs3svc_decode_setaclargs. */
121 posix_acl_release(argp->acl_access);
122 posix_acl_release(argp->acl_default);
123 return rpc_success;
124}
125
126/*
127 * XDR decode functions
128 */
129
130static bool
131nfs3svc_decode_getaclargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
132{
133 struct nfsd3_getaclargs *args = rqstp->rq_argp;
134
135 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
136 return false;
137 if (xdr_stream_decode_u32(xdr, &args->mask) < 0)
138 return false;
139
140 return true;
141}
142
143static bool
144nfs3svc_decode_setaclargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
145{
146 struct nfsd3_setaclargs *argp = rqstp->rq_argp;
147
148 if (!svcxdr_decode_nfs_fh3(xdr, &argp->fh))
149 return false;
150 if (xdr_stream_decode_u32(xdr, &argp->mask) < 0)
151 return false;
152 if (argp->mask & ~NFS_ACL_MASK)
153 return false;
154 if (!nfs_stream_decode_acl(xdr, NULL, (argp->mask & NFS_ACL) ?
155 &argp->acl_access : NULL))
156 return false;
157 if (!nfs_stream_decode_acl(xdr, NULL, (argp->mask & NFS_DFACL) ?
158 &argp->acl_default : NULL))
159 return false;
160
161 return true;
162}
163
164/*
165 * XDR encode functions
166 */
167
168/* GETACL */
169static bool
170nfs3svc_encode_getaclres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
171{
172 struct nfsd3_getaclres *resp = rqstp->rq_resp;
173 struct dentry *dentry = resp->fh.fh_dentry;
174 struct inode *inode;
175
176 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
177 return false;
178 switch (resp->status) {
179 case nfs_ok:
180 inode = d_inode(dentry);
181 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
182 return false;
183 if (xdr_stream_encode_u32(xdr, resp->mask) < 0)
184 return false;
185
186 if (!nfs_stream_encode_acl(xdr, inode, resp->acl_access,
187 resp->mask & NFS_ACL, 0))
188 return false;
189 if (!nfs_stream_encode_acl(xdr, inode, resp->acl_default,
190 resp->mask & NFS_DFACL,
191 NFS_ACL_DEFAULT))
192 return false;
193 break;
194 default:
195 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
196 return false;
197 }
198
199 return true;
200}
201
202/* SETACL */
203static bool
204nfs3svc_encode_setaclres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
205{
206 struct nfsd3_attrstat *resp = rqstp->rq_resp;
207
208 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
209 svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh);
210}
211
212/*
213 * XDR release functions
214 */
215static void nfs3svc_release_getacl(struct svc_rqst *rqstp)
216{
217 struct nfsd3_getaclres *resp = rqstp->rq_resp;
218
219 fh_put(&resp->fh);
220 posix_acl_release(resp->acl_access);
221 posix_acl_release(resp->acl_default);
222}
223
224struct nfsd3_voidargs { int dummy; };
225
226#define ST 1 /* status*/
227#define AT 21 /* attributes */
228#define pAT (1+AT) /* post attributes - conditional */
229#define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
230
231static const struct svc_procedure nfsd_acl_procedures3[3] = {
232 [ACLPROC3_NULL] = {
233 .pc_func = nfsd3_proc_null,
234 .pc_decode = nfssvc_decode_voidarg,
235 .pc_encode = nfssvc_encode_voidres,
236 .pc_argsize = sizeof(struct nfsd_voidargs),
237 .pc_argzero = sizeof(struct nfsd_voidargs),
238 .pc_ressize = sizeof(struct nfsd_voidres),
239 .pc_cachetype = RC_NOCACHE,
240 .pc_xdrressize = ST,
241 .pc_name = "NULL",
242 },
243 [ACLPROC3_GETACL] = {
244 .pc_func = nfsd3_proc_getacl,
245 .pc_decode = nfs3svc_decode_getaclargs,
246 .pc_encode = nfs3svc_encode_getaclres,
247 .pc_release = nfs3svc_release_getacl,
248 .pc_argsize = sizeof(struct nfsd3_getaclargs),
249 .pc_argzero = sizeof(struct nfsd3_getaclargs),
250 .pc_ressize = sizeof(struct nfsd3_getaclres),
251 .pc_cachetype = RC_NOCACHE,
252 .pc_xdrressize = ST+1+2*(1+ACL),
253 .pc_name = "GETACL",
254 },
255 [ACLPROC3_SETACL] = {
256 .pc_func = nfsd3_proc_setacl,
257 .pc_decode = nfs3svc_decode_setaclargs,
258 .pc_encode = nfs3svc_encode_setaclres,
259 .pc_release = nfs3svc_release_fhandle,
260 .pc_argsize = sizeof(struct nfsd3_setaclargs),
261 .pc_argzero = sizeof(struct nfsd3_setaclargs),
262 .pc_ressize = sizeof(struct nfsd3_attrstat),
263 .pc_cachetype = RC_NOCACHE,
264 .pc_xdrressize = ST+pAT,
265 .pc_name = "SETACL",
266 },
267};
268
269static DEFINE_PER_CPU_ALIGNED(unsigned long,
270 nfsd_acl_count3[ARRAY_SIZE(nfsd_acl_procedures3)]);
271const struct svc_version nfsd_acl_version3 = {
272 .vs_vers = 3,
273 .vs_nproc = ARRAY_SIZE(nfsd_acl_procedures3),
274 .vs_proc = nfsd_acl_procedures3,
275 .vs_count = nfsd_acl_count3,
276 .vs_dispatch = nfsd_dispatch,
277 .vs_xdrsize = NFS3_SVC_XDRSIZE,
278};
279