nfs_srvsubs.c revision 1.1.8.2 1 1.1.8.2 skrll /* $NetBSD: nfs_srvsubs.c,v 1.1.8.2 2009/01/19 13:20:20 skrll Exp $ */
2 1.1.8.2 skrll
3 1.1.8.2 skrll /*
4 1.1.8.2 skrll * Copyright (c) 1989, 1993
5 1.1.8.2 skrll * The Regents of the University of California. All rights reserved.
6 1.1.8.2 skrll *
7 1.1.8.2 skrll * This code is derived from software contributed to Berkeley by
8 1.1.8.2 skrll * Rick Macklem at The University of Guelph.
9 1.1.8.2 skrll *
10 1.1.8.2 skrll * Redistribution and use in source and binary forms, with or without
11 1.1.8.2 skrll * modification, are permitted provided that the following conditions
12 1.1.8.2 skrll * are met:
13 1.1.8.2 skrll * 1. Redistributions of source code must retain the above copyright
14 1.1.8.2 skrll * notice, this list of conditions and the following disclaimer.
15 1.1.8.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.8.2 skrll * notice, this list of conditions and the following disclaimer in the
17 1.1.8.2 skrll * documentation and/or other materials provided with the distribution.
18 1.1.8.2 skrll * 3. Neither the name of the University nor the names of its contributors
19 1.1.8.2 skrll * may be used to endorse or promote products derived from this software
20 1.1.8.2 skrll * without specific prior written permission.
21 1.1.8.2 skrll *
22 1.1.8.2 skrll * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1.8.2 skrll * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1.8.2 skrll * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1.8.2 skrll * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1.8.2 skrll * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1.8.2 skrll * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1.8.2 skrll * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1.8.2 skrll * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1.8.2 skrll * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1.8.2 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1.8.2 skrll * SUCH DAMAGE.
33 1.1.8.2 skrll *
34 1.1.8.2 skrll * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
35 1.1.8.2 skrll */
36 1.1.8.2 skrll
37 1.1.8.2 skrll /*
38 1.1.8.2 skrll * Copyright 2000 Wasabi Systems, Inc.
39 1.1.8.2 skrll * All rights reserved.
40 1.1.8.2 skrll *
41 1.1.8.2 skrll * Written by Frank van der Linden for Wasabi Systems, Inc.
42 1.1.8.2 skrll *
43 1.1.8.2 skrll * Redistribution and use in source and binary forms, with or without
44 1.1.8.2 skrll * modification, are permitted provided that the following conditions
45 1.1.8.2 skrll * are met:
46 1.1.8.2 skrll * 1. Redistributions of source code must retain the above copyright
47 1.1.8.2 skrll * notice, this list of conditions and the following disclaimer.
48 1.1.8.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
49 1.1.8.2 skrll * notice, this list of conditions and the following disclaimer in the
50 1.1.8.2 skrll * documentation and/or other materials provided with the distribution.
51 1.1.8.2 skrll * 3. All advertising materials mentioning features or use of this software
52 1.1.8.2 skrll * must display the following acknowledgement:
53 1.1.8.2 skrll * This product includes software developed for the NetBSD Project by
54 1.1.8.2 skrll * Wasabi Systems, Inc.
55 1.1.8.2 skrll * 4. The name of Wasabi Systems, Inc. may not be used to endorse
56 1.1.8.2 skrll * or promote products derived from this software without specific prior
57 1.1.8.2 skrll * written permission.
58 1.1.8.2 skrll *
59 1.1.8.2 skrll * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
60 1.1.8.2 skrll * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61 1.1.8.2 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62 1.1.8.2 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
63 1.1.8.2 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64 1.1.8.2 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65 1.1.8.2 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66 1.1.8.2 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67 1.1.8.2 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68 1.1.8.2 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69 1.1.8.2 skrll * POSSIBILITY OF SUCH DAMAGE.
70 1.1.8.2 skrll */
71 1.1.8.2 skrll
72 1.1.8.2 skrll #include <sys/cdefs.h>
73 1.1.8.2 skrll __KERNEL_RCSID(0, "$NetBSD: nfs_srvsubs.c,v 1.1.8.2 2009/01/19 13:20:20 skrll Exp $");
74 1.1.8.2 skrll
75 1.1.8.2 skrll #include <sys/param.h>
76 1.1.8.2 skrll #include <sys/proc.h>
77 1.1.8.2 skrll #include <sys/systm.h>
78 1.1.8.2 skrll #include <sys/kernel.h>
79 1.1.8.2 skrll #include <sys/kmem.h>
80 1.1.8.2 skrll #include <sys/mount.h>
81 1.1.8.2 skrll #include <sys/vnode.h>
82 1.1.8.2 skrll #include <sys/namei.h>
83 1.1.8.2 skrll #include <sys/mbuf.h>
84 1.1.8.2 skrll #include <sys/socket.h>
85 1.1.8.2 skrll #include <sys/stat.h>
86 1.1.8.2 skrll #include <sys/filedesc.h>
87 1.1.8.2 skrll #include <sys/time.h>
88 1.1.8.2 skrll #include <sys/dirent.h>
89 1.1.8.2 skrll #include <sys/once.h>
90 1.1.8.2 skrll #include <sys/kauth.h>
91 1.1.8.2 skrll #include <sys/atomic.h>
92 1.1.8.2 skrll
93 1.1.8.2 skrll #include <uvm/uvm_extern.h>
94 1.1.8.2 skrll
95 1.1.8.2 skrll #include <nfs/rpcv2.h>
96 1.1.8.2 skrll #include <nfs/nfsproto.h>
97 1.1.8.2 skrll #include <nfs/nfsnode.h>
98 1.1.8.2 skrll #include <nfs/nfs.h>
99 1.1.8.2 skrll #include <nfs/xdr_subs.h>
100 1.1.8.2 skrll #include <nfs/nfsm_subs.h>
101 1.1.8.2 skrll #include <nfs/nfsmount.h>
102 1.1.8.2 skrll #include <nfs/nfsrtt.h>
103 1.1.8.2 skrll #include <nfs/nfs_var.h>
104 1.1.8.2 skrll
105 1.1.8.2 skrll #include <miscfs/specfs/specdev.h>
106 1.1.8.2 skrll
107 1.1.8.2 skrll #include <netinet/in.h>
108 1.1.8.2 skrll
109 1.1.8.2 skrll /*
110 1.1.8.2 skrll * Set up nameidata for a lookup() call and do it.
111 1.1.8.2 skrll *
112 1.1.8.2 skrll * If pubflag is set, this call is done for a lookup operation on the
113 1.1.8.2 skrll * public filehandle. In that case we allow crossing mountpoints and
114 1.1.8.2 skrll * absolute pathnames. However, the caller is expected to check that
115 1.1.8.2 skrll * the lookup result is within the public fs, and deny access if
116 1.1.8.2 skrll * it is not.
117 1.1.8.2 skrll */
118 1.1.8.2 skrll int
119 1.1.8.2 skrll nfs_namei(ndp, nsfh, len, slp, nam, mdp, dposp, retdirp, l, kerbflag, pubflag)
120 1.1.8.2 skrll struct nameidata *ndp;
121 1.1.8.2 skrll nfsrvfh_t *nsfh;
122 1.1.8.2 skrll uint32_t len;
123 1.1.8.2 skrll struct nfssvc_sock *slp;
124 1.1.8.2 skrll struct mbuf *nam;
125 1.1.8.2 skrll struct mbuf **mdp;
126 1.1.8.2 skrll char **dposp;
127 1.1.8.2 skrll struct vnode **retdirp;
128 1.1.8.2 skrll struct lwp *l;
129 1.1.8.2 skrll int kerbflag, pubflag;
130 1.1.8.2 skrll {
131 1.1.8.2 skrll int i, rem;
132 1.1.8.2 skrll struct mbuf *md;
133 1.1.8.2 skrll char *fromcp, *tocp, *cp;
134 1.1.8.2 skrll struct iovec aiov;
135 1.1.8.2 skrll struct uio auio;
136 1.1.8.2 skrll struct vnode *dp;
137 1.1.8.2 skrll int error, rdonly, linklen;
138 1.1.8.2 skrll struct componentname *cnp = &ndp->ni_cnd;
139 1.1.8.2 skrll
140 1.1.8.2 skrll *retdirp = NULL;
141 1.1.8.2 skrll
142 1.1.8.2 skrll if ((len + 1) > MAXPATHLEN)
143 1.1.8.2 skrll return (ENAMETOOLONG);
144 1.1.8.2 skrll if (len == 0)
145 1.1.8.2 skrll return (EACCES);
146 1.1.8.2 skrll cnp->cn_pnbuf = PNBUF_GET();
147 1.1.8.2 skrll
148 1.1.8.2 skrll /*
149 1.1.8.2 skrll * Copy the name from the mbuf list to ndp->ni_pnbuf
150 1.1.8.2 skrll * and set the various ndp fields appropriately.
151 1.1.8.2 skrll */
152 1.1.8.2 skrll fromcp = *dposp;
153 1.1.8.2 skrll tocp = cnp->cn_pnbuf;
154 1.1.8.2 skrll md = *mdp;
155 1.1.8.2 skrll rem = mtod(md, char *) + md->m_len - fromcp;
156 1.1.8.2 skrll for (i = 0; i < len; i++) {
157 1.1.8.2 skrll while (rem == 0) {
158 1.1.8.2 skrll md = md->m_next;
159 1.1.8.2 skrll if (md == NULL) {
160 1.1.8.2 skrll error = EBADRPC;
161 1.1.8.2 skrll goto out;
162 1.1.8.2 skrll }
163 1.1.8.2 skrll fromcp = mtod(md, void *);
164 1.1.8.2 skrll rem = md->m_len;
165 1.1.8.2 skrll }
166 1.1.8.2 skrll if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
167 1.1.8.2 skrll error = EACCES;
168 1.1.8.2 skrll goto out;
169 1.1.8.2 skrll }
170 1.1.8.2 skrll *tocp++ = *fromcp++;
171 1.1.8.2 skrll rem--;
172 1.1.8.2 skrll }
173 1.1.8.2 skrll *tocp = '\0';
174 1.1.8.2 skrll *mdp = md;
175 1.1.8.2 skrll *dposp = fromcp;
176 1.1.8.2 skrll len = nfsm_rndup(len)-len;
177 1.1.8.2 skrll if (len > 0) {
178 1.1.8.2 skrll if (rem >= len)
179 1.1.8.2 skrll *dposp += len;
180 1.1.8.2 skrll else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
181 1.1.8.2 skrll goto out;
182 1.1.8.2 skrll }
183 1.1.8.2 skrll
184 1.1.8.2 skrll /*
185 1.1.8.2 skrll * Extract and set starting directory.
186 1.1.8.2 skrll */
187 1.1.8.2 skrll error = nfsrv_fhtovp(nsfh, false, &dp, ndp->ni_cnd.cn_cred, slp,
188 1.1.8.2 skrll nam, &rdonly, kerbflag, pubflag);
189 1.1.8.2 skrll if (error)
190 1.1.8.2 skrll goto out;
191 1.1.8.2 skrll if (dp->v_type != VDIR) {
192 1.1.8.2 skrll vrele(dp);
193 1.1.8.2 skrll error = ENOTDIR;
194 1.1.8.2 skrll goto out;
195 1.1.8.2 skrll }
196 1.1.8.2 skrll
197 1.1.8.2 skrll if (rdonly)
198 1.1.8.2 skrll cnp->cn_flags |= RDONLY;
199 1.1.8.2 skrll
200 1.1.8.2 skrll *retdirp = dp;
201 1.1.8.2 skrll
202 1.1.8.2 skrll if (pubflag) {
203 1.1.8.2 skrll /*
204 1.1.8.2 skrll * Oh joy. For WebNFS, handle those pesky '%' escapes,
205 1.1.8.2 skrll * and the 'native path' indicator.
206 1.1.8.2 skrll */
207 1.1.8.2 skrll cp = PNBUF_GET();
208 1.1.8.2 skrll fromcp = cnp->cn_pnbuf;
209 1.1.8.2 skrll tocp = cp;
210 1.1.8.2 skrll if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
211 1.1.8.2 skrll switch ((unsigned char)*fromcp) {
212 1.1.8.2 skrll case WEBNFS_NATIVE_CHAR:
213 1.1.8.2 skrll /*
214 1.1.8.2 skrll * 'Native' path for us is the same
215 1.1.8.2 skrll * as a path according to the NFS spec,
216 1.1.8.2 skrll * just skip the escape char.
217 1.1.8.2 skrll */
218 1.1.8.2 skrll fromcp++;
219 1.1.8.2 skrll break;
220 1.1.8.2 skrll /*
221 1.1.8.2 skrll * More may be added in the future, range 0x80-0xff
222 1.1.8.2 skrll */
223 1.1.8.2 skrll default:
224 1.1.8.2 skrll error = EIO;
225 1.1.8.2 skrll vrele(dp);
226 1.1.8.2 skrll PNBUF_PUT(cp);
227 1.1.8.2 skrll goto out;
228 1.1.8.2 skrll }
229 1.1.8.2 skrll }
230 1.1.8.2 skrll /*
231 1.1.8.2 skrll * Translate the '%' escapes, URL-style.
232 1.1.8.2 skrll */
233 1.1.8.2 skrll while (*fromcp != '\0') {
234 1.1.8.2 skrll if (*fromcp == WEBNFS_ESC_CHAR) {
235 1.1.8.2 skrll if (fromcp[1] != '\0' && fromcp[2] != '\0') {
236 1.1.8.2 skrll fromcp++;
237 1.1.8.2 skrll *tocp++ = HEXSTRTOI(fromcp);
238 1.1.8.2 skrll fromcp += 2;
239 1.1.8.2 skrll continue;
240 1.1.8.2 skrll } else {
241 1.1.8.2 skrll error = ENOENT;
242 1.1.8.2 skrll vrele(dp);
243 1.1.8.2 skrll PNBUF_PUT(cp);
244 1.1.8.2 skrll goto out;
245 1.1.8.2 skrll }
246 1.1.8.2 skrll } else
247 1.1.8.2 skrll *tocp++ = *fromcp++;
248 1.1.8.2 skrll }
249 1.1.8.2 skrll *tocp = '\0';
250 1.1.8.2 skrll PNBUF_PUT(cnp->cn_pnbuf);
251 1.1.8.2 skrll cnp->cn_pnbuf = cp;
252 1.1.8.2 skrll }
253 1.1.8.2 skrll
254 1.1.8.2 skrll ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
255 1.1.8.2 skrll ndp->ni_segflg = UIO_SYSSPACE;
256 1.1.8.2 skrll ndp->ni_rootdir = rootvnode;
257 1.1.8.2 skrll ndp->ni_erootdir = NULL;
258 1.1.8.2 skrll
259 1.1.8.2 skrll if (pubflag) {
260 1.1.8.2 skrll ndp->ni_loopcnt = 0;
261 1.1.8.2 skrll if (cnp->cn_pnbuf[0] == '/')
262 1.1.8.2 skrll dp = rootvnode;
263 1.1.8.2 skrll } else {
264 1.1.8.2 skrll cnp->cn_flags |= NOCROSSMOUNT;
265 1.1.8.2 skrll }
266 1.1.8.2 skrll
267 1.1.8.2 skrll VREF(dp);
268 1.1.8.2 skrll vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
269 1.1.8.2 skrll
270 1.1.8.2 skrll for (;;) {
271 1.1.8.2 skrll cnp->cn_nameptr = cnp->cn_pnbuf;
272 1.1.8.2 skrll ndp->ni_startdir = dp;
273 1.1.8.2 skrll
274 1.1.8.2 skrll /*
275 1.1.8.2 skrll * And call lookup() to do the real work
276 1.1.8.2 skrll */
277 1.1.8.2 skrll error = lookup(ndp);
278 1.1.8.2 skrll if (error) {
279 1.1.8.2 skrll if (ndp->ni_dvp) {
280 1.1.8.2 skrll vput(ndp->ni_dvp);
281 1.1.8.2 skrll }
282 1.1.8.2 skrll PNBUF_PUT(cnp->cn_pnbuf);
283 1.1.8.2 skrll return (error);
284 1.1.8.2 skrll }
285 1.1.8.2 skrll
286 1.1.8.2 skrll /*
287 1.1.8.2 skrll * Check for encountering a symbolic link
288 1.1.8.2 skrll */
289 1.1.8.2 skrll if ((cnp->cn_flags & ISSYMLINK) == 0) {
290 1.1.8.2 skrll if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp) {
291 1.1.8.2 skrll if (ndp->ni_dvp == ndp->ni_vp) {
292 1.1.8.2 skrll vrele(ndp->ni_dvp);
293 1.1.8.2 skrll } else {
294 1.1.8.2 skrll vput(ndp->ni_dvp);
295 1.1.8.2 skrll }
296 1.1.8.2 skrll }
297 1.1.8.2 skrll if (cnp->cn_flags & (SAVENAME | SAVESTART))
298 1.1.8.2 skrll cnp->cn_flags |= HASBUF;
299 1.1.8.2 skrll else
300 1.1.8.2 skrll PNBUF_PUT(cnp->cn_pnbuf);
301 1.1.8.2 skrll return (0);
302 1.1.8.2 skrll } else {
303 1.1.8.2 skrll if (!pubflag) {
304 1.1.8.2 skrll error = EINVAL;
305 1.1.8.2 skrll break;
306 1.1.8.2 skrll }
307 1.1.8.2 skrll if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
308 1.1.8.2 skrll error = ELOOP;
309 1.1.8.2 skrll break;
310 1.1.8.2 skrll }
311 1.1.8.2 skrll if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
312 1.1.8.2 skrll error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred);
313 1.1.8.2 skrll if (error != 0)
314 1.1.8.2 skrll break;
315 1.1.8.2 skrll }
316 1.1.8.2 skrll if (ndp->ni_pathlen > 1)
317 1.1.8.2 skrll cp = PNBUF_GET();
318 1.1.8.2 skrll else
319 1.1.8.2 skrll cp = cnp->cn_pnbuf;
320 1.1.8.2 skrll aiov.iov_base = cp;
321 1.1.8.2 skrll aiov.iov_len = MAXPATHLEN;
322 1.1.8.2 skrll auio.uio_iov = &aiov;
323 1.1.8.2 skrll auio.uio_iovcnt = 1;
324 1.1.8.2 skrll auio.uio_offset = 0;
325 1.1.8.2 skrll auio.uio_rw = UIO_READ;
326 1.1.8.2 skrll auio.uio_resid = MAXPATHLEN;
327 1.1.8.2 skrll UIO_SETUP_SYSSPACE(&auio);
328 1.1.8.2 skrll error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
329 1.1.8.2 skrll if (error) {
330 1.1.8.2 skrll badlink:
331 1.1.8.2 skrll if (ndp->ni_pathlen > 1)
332 1.1.8.2 skrll PNBUF_PUT(cp);
333 1.1.8.2 skrll break;
334 1.1.8.2 skrll }
335 1.1.8.2 skrll linklen = MAXPATHLEN - auio.uio_resid;
336 1.1.8.2 skrll if (linklen == 0) {
337 1.1.8.2 skrll error = ENOENT;
338 1.1.8.2 skrll goto badlink;
339 1.1.8.2 skrll }
340 1.1.8.2 skrll if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
341 1.1.8.2 skrll error = ENAMETOOLONG;
342 1.1.8.2 skrll goto badlink;
343 1.1.8.2 skrll }
344 1.1.8.2 skrll if (ndp->ni_pathlen > 1) {
345 1.1.8.2 skrll memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
346 1.1.8.2 skrll PNBUF_PUT(cnp->cn_pnbuf);
347 1.1.8.2 skrll cnp->cn_pnbuf = cp;
348 1.1.8.2 skrll } else
349 1.1.8.2 skrll cnp->cn_pnbuf[linklen] = '\0';
350 1.1.8.2 skrll ndp->ni_pathlen += linklen;
351 1.1.8.2 skrll vput(ndp->ni_vp);
352 1.1.8.2 skrll dp = ndp->ni_dvp;
353 1.1.8.2 skrll
354 1.1.8.2 skrll /*
355 1.1.8.2 skrll * Check if root directory should replace current directory.
356 1.1.8.2 skrll */
357 1.1.8.2 skrll if (cnp->cn_pnbuf[0] == '/') {
358 1.1.8.2 skrll vput(dp);
359 1.1.8.2 skrll dp = ndp->ni_rootdir;
360 1.1.8.2 skrll VREF(dp);
361 1.1.8.2 skrll vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
362 1.1.8.2 skrll }
363 1.1.8.2 skrll }
364 1.1.8.2 skrll }
365 1.1.8.2 skrll vput(ndp->ni_dvp);
366 1.1.8.2 skrll vput(ndp->ni_vp);
367 1.1.8.2 skrll ndp->ni_vp = NULL;
368 1.1.8.2 skrll out:
369 1.1.8.2 skrll PNBUF_PUT(cnp->cn_pnbuf);
370 1.1.8.2 skrll return (error);
371 1.1.8.2 skrll }
372 1.1.8.2 skrll
373 1.1.8.2 skrll /*
374 1.1.8.2 skrll * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
375 1.1.8.2 skrll * - look up fsid in mount list (if not found ret error)
376 1.1.8.2 skrll * - get vp and export rights by calling VFS_FHTOVP()
377 1.1.8.2 skrll * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
378 1.1.8.2 skrll * - if not lockflag unlock it with VOP_UNLOCK()
379 1.1.8.2 skrll */
380 1.1.8.2 skrll int
381 1.1.8.2 skrll nfsrv_fhtovp(nfsrvfh_t *nsfh, int lockflag, struct vnode **vpp,
382 1.1.8.2 skrll kauth_cred_t cred, struct nfssvc_sock *slp, struct mbuf *nam, int *rdonlyp,
383 1.1.8.2 skrll int kerbflag, int pubflag)
384 1.1.8.2 skrll {
385 1.1.8.2 skrll struct mount *mp;
386 1.1.8.2 skrll kauth_cred_t credanon;
387 1.1.8.2 skrll int error, exflags;
388 1.1.8.2 skrll struct sockaddr_in *saddr;
389 1.1.8.2 skrll fhandle_t *fhp;
390 1.1.8.2 skrll
391 1.1.8.2 skrll fhp = NFSRVFH_FHANDLE(nsfh);
392 1.1.8.2 skrll *vpp = (struct vnode *)0;
393 1.1.8.2 skrll
394 1.1.8.2 skrll if (nfs_ispublicfh(nsfh)) {
395 1.1.8.2 skrll if (!pubflag || !nfs_pub.np_valid)
396 1.1.8.2 skrll return (ESTALE);
397 1.1.8.2 skrll fhp = nfs_pub.np_handle;
398 1.1.8.2 skrll }
399 1.1.8.2 skrll
400 1.1.8.2 skrll error = netexport_check(&fhp->fh_fsid, nam, &mp, &exflags, &credanon);
401 1.1.8.2 skrll if (error) {
402 1.1.8.2 skrll return error;
403 1.1.8.2 skrll }
404 1.1.8.2 skrll
405 1.1.8.2 skrll error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
406 1.1.8.2 skrll if (error)
407 1.1.8.2 skrll return (error);
408 1.1.8.2 skrll
409 1.1.8.2 skrll if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
410 1.1.8.2 skrll saddr = mtod(nam, struct sockaddr_in *);
411 1.1.8.2 skrll if ((saddr->sin_family == AF_INET) &&
412 1.1.8.2 skrll ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
413 1.1.8.2 skrll vput(*vpp);
414 1.1.8.2 skrll return (NFSERR_AUTHERR | AUTH_TOOWEAK);
415 1.1.8.2 skrll }
416 1.1.8.2 skrll if ((saddr->sin_family == AF_INET6) &&
417 1.1.8.2 skrll ntohs(saddr->sin_port) >= IPV6PORT_RESERVED) {
418 1.1.8.2 skrll vput(*vpp);
419 1.1.8.2 skrll return (NFSERR_AUTHERR | AUTH_TOOWEAK);
420 1.1.8.2 skrll }
421 1.1.8.2 skrll }
422 1.1.8.2 skrll /*
423 1.1.8.2 skrll * Check/setup credentials.
424 1.1.8.2 skrll */
425 1.1.8.2 skrll if (exflags & MNT_EXKERB) {
426 1.1.8.2 skrll if (!kerbflag) {
427 1.1.8.2 skrll vput(*vpp);
428 1.1.8.2 skrll return (NFSERR_AUTHERR | AUTH_TOOWEAK);
429 1.1.8.2 skrll }
430 1.1.8.2 skrll } else if (kerbflag) {
431 1.1.8.2 skrll vput(*vpp);
432 1.1.8.2 skrll return (NFSERR_AUTHERR | AUTH_TOOWEAK);
433 1.1.8.2 skrll } else if (kauth_cred_geteuid(cred) == 0 || /* NFS maproot, see below */
434 1.1.8.2 skrll (exflags & MNT_EXPORTANON)) {
435 1.1.8.2 skrll /*
436 1.1.8.2 skrll * This is used by the NFS maproot option. While we can change
437 1.1.8.2 skrll * the secmodel on our own host, we can't change it on the
438 1.1.8.2 skrll * clients. As means of least surprise, we're doing the
439 1.1.8.2 skrll * traditional thing here.
440 1.1.8.2 skrll * Should look into adding a "mapprivileged" or similar where
441 1.1.8.2 skrll * the users can be explicitly specified...
442 1.1.8.2 skrll * [elad, yamt 2008-03-05]
443 1.1.8.2 skrll */
444 1.1.8.2 skrll kauth_cred_clone(credanon, cred);
445 1.1.8.2 skrll }
446 1.1.8.2 skrll if (exflags & MNT_EXRDONLY)
447 1.1.8.2 skrll *rdonlyp = 1;
448 1.1.8.2 skrll else
449 1.1.8.2 skrll *rdonlyp = 0;
450 1.1.8.2 skrll if (!lockflag)
451 1.1.8.2 skrll VOP_UNLOCK(*vpp, 0);
452 1.1.8.2 skrll return (0);
453 1.1.8.2 skrll }
454 1.1.8.2 skrll
455 1.1.8.2 skrll /*
456 1.1.8.2 skrll * WebNFS: check if a filehandle is a public filehandle. For v3, this
457 1.1.8.2 skrll * means a length of 0, for v2 it means all zeroes.
458 1.1.8.2 skrll */
459 1.1.8.2 skrll int
460 1.1.8.2 skrll nfs_ispublicfh(const nfsrvfh_t *nsfh)
461 1.1.8.2 skrll {
462 1.1.8.2 skrll const char *cp = (const void *)(NFSRVFH_DATA(nsfh));
463 1.1.8.2 skrll int i;
464 1.1.8.2 skrll
465 1.1.8.2 skrll if (NFSRVFH_SIZE(nsfh) == 0) {
466 1.1.8.2 skrll return true;
467 1.1.8.2 skrll }
468 1.1.8.2 skrll if (NFSRVFH_SIZE(nsfh) != NFSX_V2FH) {
469 1.1.8.2 skrll return false;
470 1.1.8.2 skrll }
471 1.1.8.2 skrll for (i = 0; i < NFSX_V2FH; i++)
472 1.1.8.2 skrll if (*cp++ != 0)
473 1.1.8.2 skrll return false;
474 1.1.8.2 skrll return true;
475 1.1.8.2 skrll }
476 1.1.8.2 skrll
477 1.1.8.2 skrll int
478 1.1.8.2 skrll nfsrv_composefh(struct vnode *vp, nfsrvfh_t *nsfh, bool v3)
479 1.1.8.2 skrll {
480 1.1.8.2 skrll int error;
481 1.1.8.2 skrll size_t fhsize;
482 1.1.8.2 skrll
483 1.1.8.2 skrll fhsize = NFSD_MAXFHSIZE;
484 1.1.8.2 skrll error = vfs_composefh(vp, (void *)NFSRVFH_DATA(nsfh), &fhsize);
485 1.1.8.2 skrll if (NFSX_FHTOOBIG_P(fhsize, v3)) {
486 1.1.8.2 skrll error = EOPNOTSUPP;
487 1.1.8.2 skrll }
488 1.1.8.2 skrll if (error != 0) {
489 1.1.8.2 skrll return error;
490 1.1.8.2 skrll }
491 1.1.8.2 skrll if (!v3 && fhsize < NFSX_V2FH) {
492 1.1.8.2 skrll memset((char *)NFSRVFH_DATA(nsfh) + fhsize, 0,
493 1.1.8.2 skrll NFSX_V2FH - fhsize);
494 1.1.8.2 skrll fhsize = NFSX_V2FH;
495 1.1.8.2 skrll }
496 1.1.8.2 skrll if ((fhsize % NFSX_UNSIGNED) != 0) {
497 1.1.8.2 skrll return EOPNOTSUPP;
498 1.1.8.2 skrll }
499 1.1.8.2 skrll nsfh->nsfh_size = fhsize;
500 1.1.8.2 skrll return 0;
501 1.1.8.2 skrll }
502 1.1.8.2 skrll
503 1.1.8.2 skrll int
504 1.1.8.2 skrll nfsrv_comparefh(const nfsrvfh_t *fh1, const nfsrvfh_t *fh2)
505 1.1.8.2 skrll {
506 1.1.8.2 skrll
507 1.1.8.2 skrll if (NFSRVFH_SIZE(fh1) != NFSRVFH_SIZE(fh2)) {
508 1.1.8.2 skrll return NFSRVFH_SIZE(fh2) - NFSRVFH_SIZE(fh1);
509 1.1.8.2 skrll }
510 1.1.8.2 skrll return memcmp(NFSRVFH_DATA(fh1), NFSRVFH_DATA(fh2), NFSRVFH_SIZE(fh1));
511 1.1.8.2 skrll }
512 1.1.8.2 skrll
513 1.1.8.2 skrll void
514 1.1.8.2 skrll nfsrv_copyfh(nfsrvfh_t *fh1, const nfsrvfh_t *fh2)
515 1.1.8.2 skrll {
516 1.1.8.2 skrll size_t size;
517 1.1.8.2 skrll
518 1.1.8.2 skrll fh1->nsfh_size = size = NFSRVFH_SIZE(fh2);
519 1.1.8.2 skrll memcpy(NFSRVFH_DATA(fh1), NFSRVFH_DATA(fh2), size);
520 1.1.8.2 skrll }
521