xref: /illumos-gate/usr/src/uts/common/fs/smbclnt/smbfs/smbfs_subr.c (revision 598f4ceed9327d2d6c2325dd67cae3aa06f7fea6)
1 /*
2  * Copyright (c) 2000-2001, Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: smbfs_subr.c,v 1.18 2005/02/02 00:22:23 lindak Exp $
33  */
34 
35 /*
36  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
37  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
38  * Use is subject to license terms.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/time.h>
44 #include <sys/vnode.h>
45 #include <sys/sunddi.h>
46 
47 #include <netsmb/smb_osdep.h>
48 
49 #include <netsmb/smb.h>
50 #include <netsmb/smb_conn.h>
51 #include <netsmb/smb_subr.h>
52 #include <netsmb/smb_rq.h>
53 
54 #include <smbfs/smbfs.h>
55 #include <smbfs/smbfs_node.h>
56 #include <smbfs/smbfs_subr.h>
57 
58 /*
59  * In the Darwin code, this function used to compute the full path
60  * by following the chain of n_parent pointers back to the root.
61  * In the Solaris port we found the n_parent pointers inconvenient
62  * because they hold parent nodes busy.  We now keep the full path
63  * in every node, so this function need only marshall the directory
64  * path, and (if provided) the separator and last component name.
65  *
66  * Note that this logic must match that in smbfs_getino
67  */
68 int
69 smbfs_fullpath(struct mbchain *mbp, struct smb_vc *vcp, struct smbnode *dnp,
70 	const char *name, int nmlen, u_int8_t sep)
71 {
72 	int caseopt = SMB_CS_NONE;
73 	int unicode = (SMB_UNICODE_STRINGS(vcp)) ? 1 : 0;
74 	int error;
75 
76 	if (SMB_DIALECT(vcp) < SMB_DIALECT_LANMAN1_0)
77 		caseopt |= SMB_CS_UPPER;
78 
79 	if (unicode) {
80 		error = mb_put_padbyte(mbp);
81 		if (error)
82 			return (error);
83 	}
84 
85 	error = smb_put_dmem(mbp, vcp,
86 	    dnp->n_rpath, dnp->n_rplen,
87 	    caseopt, NULL);
88 	if (name) {
89 		/*
90 		 * Special case at share root:
91 		 * Don't put another slash.
92 		 */
93 		if (dnp->n_rplen <= 1 && sep == '\\')
94 			sep = 0;
95 		/*
96 		 * More special cases, now for XATTR:
97 		 * Our "faked up" XATTR directories use a
98 		 * full path name ending with ":" so as to
99 		 * avoid conflicts with any real paths.
100 		 * (It is not a valid CIFS path name.)
101 		 * Therefore, when we're composing a full
102 		 * path name from an XATTR directory, we
103 		 * need to _ommit_ the ":" separator and
104 		 * instead copy the one from the "fake"
105 		 * parent node's path name.
106 		 */
107 		if (dnp->n_flag & N_XATTR)
108 			sep = 0;
109 
110 		if (sep) {
111 			/* Put the separator */
112 			if (unicode)
113 				error = mb_put_uint16le(mbp, sep);
114 			else
115 				error = mb_put_uint8(mbp, sep);
116 			if (error)
117 				return (error);
118 		}
119 		/* Put the name */
120 		error = smb_put_dmem(mbp, vcp,
121 		    name, nmlen, caseopt, NULL);
122 		if (error)
123 			return (error);
124 	}
125 	/* Put NULL termination. */
126 	if (unicode)
127 		error = mb_put_uint16le(mbp, 0);
128 	else
129 		error = mb_put_uint8(mbp, 0);
130 
131 	return (error);
132 }
133 
134 /*
135  * Convert a Unicode directory entry to UTF-8
136  */
137 void
138 smbfs_fname_tolocal(struct smbfs_fctx *ctx)
139 {
140 	uchar_t tmpbuf[SMB_MAXFNAMELEN+1];
141 	struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
142 	uchar_t *dst;
143 	const ushort_t *src;
144 	size_t inlen, outlen;
145 	int flags;
146 
147 	if (ctx->f_nmlen == 0)
148 		return;
149 
150 	if (!SMB_UNICODE_STRINGS(vcp))
151 		return;
152 
153 	if (ctx->f_namesz < sizeof (tmpbuf)) {
154 		ASSERT(0);
155 		goto errout;
156 	}
157 
158 	/*
159 	 * In-place conversions are not supported,
160 	 * so convert into tmpbuf and copy.
161 	 */
162 	dst = tmpbuf;
163 	outlen = SMB_MAXFNAMELEN;
164 	/*LINTED*/
165 	src = (const ushort_t *)ctx->f_name;
166 	inlen = ctx->f_nmlen / 2;	/* number of UCS-2 characters */
167 	flags = UCONV_IN_LITTLE_ENDIAN;
168 
169 	if (uconv_u16tou8(src, &inlen, dst, &outlen, flags) != 0)
170 		goto errout;
171 
172 	ASSERT(outlen < sizeof (tmpbuf));
173 	tmpbuf[outlen] = '\0';
174 	bcopy(tmpbuf, ctx->f_name, outlen + 1);
175 	ctx->f_nmlen = (int)outlen;
176 	return;
177 
178 errout:
179 	/*
180 	 * Conversion failed, but our caller does not
181 	 * deal with errors here, so just put a "?".
182 	 * Don't expect to ever see this.
183 	 */
184 	(void) strlcpy(ctx->f_name, "?", ctx->f_namesz);
185 }
186