1*8329232eSGordon Ross /*
2*8329232eSGordon Ross  * CDDL HEADER START
3*8329232eSGordon Ross  *
4*8329232eSGordon Ross  * The contents of this file are subject to the terms of the
5*8329232eSGordon Ross  * Common Development and Distribution License (the "License").
6*8329232eSGordon Ross  * You may not use this file except in compliance with the License.
7*8329232eSGordon Ross  *
8*8329232eSGordon Ross  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*8329232eSGordon Ross  * or http://www.opensolaris.org/os/licensing.
10*8329232eSGordon Ross  * See the License for the specific language governing permissions
11*8329232eSGordon Ross  * and limitations under the License.
12*8329232eSGordon Ross  *
13*8329232eSGordon Ross  * When distributing Covered Code, include this CDDL HEADER in each
14*8329232eSGordon Ross  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*8329232eSGordon Ross  * If applicable, add the following below this CDDL HEADER, with the
16*8329232eSGordon Ross  * fields enclosed by brackets "[]" replaced with your own identifying
17*8329232eSGordon Ross  * information: Portions Copyright [yyyy] [name of copyright owner]
18*8329232eSGordon Ross  *
19*8329232eSGordon Ross  * CDDL HEADER END
20*8329232eSGordon Ross  */
21*8329232eSGordon Ross 
22*8329232eSGordon Ross /*
23*8329232eSGordon Ross  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24*8329232eSGordon Ross  * Copyright 2016 Joyent, Inc.
25*8329232eSGordon Ross  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
26*8329232eSGordon Ross  */
27*8329232eSGordon Ross 
28*8329232eSGordon Ross /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
29*8329232eSGordon Ross /*	  All Rights Reserved	*/
30*8329232eSGordon Ross 
31*8329232eSGordon Ross /*
32*8329232eSGordon Ross  * University Copyright- Copyright (c) 1982, 1986, 1988
33*8329232eSGordon Ross  * The Regents of the University of California
34*8329232eSGordon Ross  * All Rights Reserved
35*8329232eSGordon Ross  *
36*8329232eSGordon Ross  * University Acknowledgment- Portions of this document are derived from
37*8329232eSGordon Ross  * software developed by the University of California, Berkeley, and its
38*8329232eSGordon Ross  * contributors.
39*8329232eSGordon Ross  */
40*8329232eSGordon Ross 
41*8329232eSGordon Ross #include <sys/types.h>
42*8329232eSGordon Ross #include <sys/param.h>
43*8329232eSGordon Ross #include <sys/systm.h>
44*8329232eSGordon Ross #include <sys/errno.h>
45*8329232eSGordon Ross #include <sys/cred.h>
46*8329232eSGordon Ross #include <sys/user.h>
47*8329232eSGordon Ross #include <sys/uio.h>
48*8329232eSGordon Ross #include <sys/vfs.h>
49*8329232eSGordon Ross #include <sys/vnode.h>
50*8329232eSGordon Ross #include <sys/pathname.h>
51*8329232eSGordon Ross #include <sys/proc.h>
52*8329232eSGordon Ross #include <sys/vtrace.h>
53*8329232eSGordon Ross #include <sys/sysmacros.h>
54*8329232eSGordon Ross #include <sys/debug.h>
55*8329232eSGordon Ross #include <sys/dirent.h>
56*8329232eSGordon Ross #include <sys/zone.h>
57*8329232eSGordon Ross #include <sys/fs/snode.h>
58*8329232eSGordon Ross 
59*8329232eSGordon Ross #include <libfksmbfs.h>
60*8329232eSGordon Ross 
61*8329232eSGordon Ross extern vnode_t *rootdir;
62*8329232eSGordon Ross 
63*8329232eSGordon Ross /*
64*8329232eSGordon Ross  * Simplified variation on lookuppnvp()
65*8329232eSGordon Ross  */
66*8329232eSGordon Ross int
fake_lookup(vnode_t * dvp,char * path,vnode_t ** vpp)67*8329232eSGordon Ross fake_lookup(vnode_t *dvp, char *path, vnode_t **vpp)
68*8329232eSGordon Ross {
69*8329232eSGordon Ross 	char component[MAXNAMELEN];	/* buffer for component */
70*8329232eSGordon Ross 	pathname_t pn;
71*8329232eSGordon Ross 	cred_t *cr;
72*8329232eSGordon Ross 	vnode_t *cvp;	/* current component vp */
73*8329232eSGordon Ross 	vnode_t *nvp;	/* next component vp */
74*8329232eSGordon Ross 	char *p;
75*8329232eSGordon Ross 	int flags = 0;
76*8329232eSGordon Ross 	int error, len;
77*8329232eSGordon Ross 
78*8329232eSGordon Ross 	bzero(&pn, sizeof (pn));
79*8329232eSGordon Ross 	pn.pn_buf = path;
80*8329232eSGordon Ross 	pn.pn_path = path;
81*8329232eSGordon Ross 	pn.pn_pathlen = strlen(path);
82*8329232eSGordon Ross 	pn.pn_bufsize = pn.pn_pathlen + 1;
83*8329232eSGordon Ross 	p = path;
84*8329232eSGordon Ross 
85*8329232eSGordon Ross 	cr = CRED();
86*8329232eSGordon Ross 	cvp = (dvp != NULL) ? dvp : rootdir;
87*8329232eSGordon Ross 	VN_HOLD(cvp);
88*8329232eSGordon Ross 	nvp = NULL;
89*8329232eSGordon Ross 
90*8329232eSGordon Ross 	while (*p != '\0') {
91*8329232eSGordon Ross 		if (*p == '/') {
92*8329232eSGordon Ross 			p++;
93*8329232eSGordon Ross 			continue;
94*8329232eSGordon Ross 		}
95*8329232eSGordon Ross 
96*8329232eSGordon Ross 		len = strcspn(p, "/");
97*8329232eSGordon Ross 		ASSERT(len > 0);
98*8329232eSGordon Ross 		if (len >= MAXNAMELEN)
99*8329232eSGordon Ross 			return (EINVAL);
100*8329232eSGordon Ross 		(void) strncpy(component, p, len);
101*8329232eSGordon Ross 		component[len] = '\0';
102*8329232eSGordon Ross 		pn.pn_path = p;
103*8329232eSGordon Ross 		pn.pn_pathlen = strlen(p);
104*8329232eSGordon Ross 
105*8329232eSGordon Ross 		error = VOP_LOOKUP(cvp, component, &nvp, &pn, flags,
106*8329232eSGordon Ross 		    rootdir, cr, NULL, NULL, NULL);
107*8329232eSGordon Ross 		VN_RELE(cvp);
108*8329232eSGordon Ross 		if (error != 0)
109*8329232eSGordon Ross 			return (error);
110*8329232eSGordon Ross 
111*8329232eSGordon Ross 		/* Lookup gave us a hold on nvp */
112*8329232eSGordon Ross 		cvp = nvp;
113*8329232eSGordon Ross 		nvp = NULL;
114*8329232eSGordon Ross 		p += len;
115*8329232eSGordon Ross 	}
116*8329232eSGordon Ross 
117*8329232eSGordon Ross 	*vpp = cvp;
118*8329232eSGordon Ross 	return (0);
119*8329232eSGordon Ross }
120*8329232eSGordon Ross 
121*8329232eSGordon Ross /*
122*8329232eSGordon Ross  * Lookup the directory and find the start of the
123*8329232eSGordon Ross  * last component of the given path.
124*8329232eSGordon Ross  */
125*8329232eSGordon Ross int
fake_lookup_dir(char * path,vnode_t ** vpp,char ** lastcomp)126*8329232eSGordon Ross fake_lookup_dir(char *path, vnode_t **vpp, char **lastcomp)
127*8329232eSGordon Ross {
128*8329232eSGordon Ross 	vnode_t *dvp;
129*8329232eSGordon Ross 	char *last;
130*8329232eSGordon Ross 	char *tpn = NULL;
131*8329232eSGordon Ross 	int tpn_sz;
132*8329232eSGordon Ross 	int lc_off;
133*8329232eSGordon Ross 	int error;
134*8329232eSGordon Ross 
135*8329232eSGordon Ross 	*vpp = NULL;
136*8329232eSGordon Ross 	*lastcomp = NULL;
137*8329232eSGordon Ross 
138*8329232eSGordon Ross 	tpn_sz = strlen(path) + 1;
139*8329232eSGordon Ross 	tpn = kmem_alloc(tpn_sz, KM_SLEEP);
140*8329232eSGordon Ross 
141*8329232eSGordon Ross 	/*
142*8329232eSGordon Ross 	 * Get a copy of the path, and zap the last /
143*8329232eSGordon Ross 	 */
144*8329232eSGordon Ross 	bcopy(path, tpn, tpn_sz);
145*8329232eSGordon Ross 	last = strrchr(tpn, '/');
146*8329232eSGordon Ross 	if (last == NULL) {
147*8329232eSGordon Ross 		lc_off = 0;
148*8329232eSGordon Ross 		dvp = rootdir;
149*8329232eSGordon Ross 		VN_HOLD(dvp);
150*8329232eSGordon Ross 		error = 0;
151*8329232eSGordon Ross 	} else {
152*8329232eSGordon Ross 		*last++ = '\0';
153*8329232eSGordon Ross 		if (*last == '\0') {
154*8329232eSGordon Ross 			error = EINVAL;
155*8329232eSGordon Ross 			goto out;
156*8329232eSGordon Ross 		}
157*8329232eSGordon Ross 		error = fake_lookup(rootdir, tpn, &dvp);
158*8329232eSGordon Ross 		if (error != 0) {
159*8329232eSGordon Ross 			/* dir not found */
160*8329232eSGordon Ross 			goto out;
161*8329232eSGordon Ross 		}
162*8329232eSGordon Ross 		lc_off = last - tpn;
163*8329232eSGordon Ross 		ASSERT(lc_off >= 0 && lc_off < tpn_sz);
164*8329232eSGordon Ross 	}
165*8329232eSGordon Ross 	*vpp = dvp;
166*8329232eSGordon Ross 	*lastcomp = path + lc_off;
167*8329232eSGordon Ross 
168*8329232eSGordon Ross out:
169*8329232eSGordon Ross 	if (tpn != NULL)
170*8329232eSGordon Ross 		kmem_free(tpn, tpn_sz);
171*8329232eSGordon Ross 
172*8329232eSGordon Ross 	return (error);
173*8329232eSGordon Ross }