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 2009 Sun Microsystems, Inc. All rights reserved.
37 * Use is subject to license terms.
38 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/time.h>
43 #include <sys/vnode.h>
44 #include <sys/sunddi.h>
45
46 #include <netsmb/smb_osdep.h>
47
48 #include <netsmb/smb.h>
49 #include <netsmb/smb_conn.h>
50 #include <netsmb/smb_subr.h>
51 #include <netsmb/smb_rq.h>
52
53 #include <smbfs/smbfs.h>
54 #include <smbfs/smbfs_node.h>
55 #include <smbfs/smbfs_subr.h>
56
57 /*
58 * In the Darwin code, this function used to compute the full path
59 * by following the chain of n_parent pointers back to the root.
60 * In the Solaris port we found the n_parent pointers inconvenient
61 * because they hold parent nodes busy. We now keep the full path
62 * in every node, so this function need only marshall the directory
63 * path, and (if provided) the separator and last component name.
64 *
65 * Note that this logic must match that in smbfs_getino
66 */
67 int
smbfs_fullpath(struct mbchain * mbp,struct smb_vc * vcp,struct smbnode * dnp,const char * name,int * lenp,u_int8_t sep)68 smbfs_fullpath(struct mbchain *mbp, struct smb_vc *vcp, struct smbnode *dnp,
69 const char *name, int *lenp, u_int8_t sep)
70 {
71 int caseopt = SMB_CS_NONE;
72 int error, len = 0;
73 int unicode = (SMB_UNICODE_STRINGS(vcp)) ? 1 : 0;
74
75 if (SMB_DIALECT(vcp) < SMB_DIALECT_LANMAN1_0)
76 caseopt |= SMB_CS_UPPER;
77
78 if (lenp) {
79 len = *lenp;
80 *lenp = 0;
81 }
82 if (unicode) {
83 error = mb_put_padbyte(mbp);
84 if (error)
85 return (error);
86 }
87
88 error = smb_put_dmem(mbp, vcp,
89 dnp->n_rpath, dnp->n_rplen,
90 caseopt, lenp);
91 if (name) {
92 /*
93 * Special case at share root:
94 * Don't put another slash.
95 */
96 if (dnp->n_rplen <= 1 && sep == '\\')
97 sep = 0;
98 /*
99 * More special cases, now for XATTR:
100 * Our "faked up" XATTR directories use a
101 * full path name ending with ":" so as to
102 * avoid conflicts with any real paths.
103 * (It is not a valid CIFS path name.)
104 * Therefore, when we're composing a full
105 * path name from an XATTR directory, we
106 * need to _ommit_ the ":" separator and
107 * instead copy the one from the "fake"
108 * parent node's path name.
109 */
110 if (dnp->n_flag & N_XATTR)
111 sep = 0;
112
113 if (sep) {
114 /* Put the separator */
115 if (unicode)
116 error = mb_put_uint16le(mbp, sep);
117 else
118 error = mb_put_uint8(mbp, sep);
119 if (!error && lenp)
120 *lenp += (unicode + 1);
121 if (error)
122 return (error);
123 }
124 /* Put the name */
125 error = smb_put_dmem(mbp, vcp,
126 name, len, caseopt, lenp);
127 if (error)
128 return (error);
129 }
130 /* Put NULL termination. */
131 if (unicode)
132 error = mb_put_uint16le(mbp, 0);
133 else
134 error = mb_put_uint8(mbp, 0);
135 if (!error && lenp)
136 *lenp += (unicode + 1);
137
138 return (error);
139 }
140
141 /*
142 * Convert a Unicode directory entry to UTF-8
143 */
144 void
smbfs_fname_tolocal(struct smbfs_fctx * ctx)145 smbfs_fname_tolocal(struct smbfs_fctx *ctx)
146 {
147 uchar_t tmpbuf[SMB_MAXFNAMELEN+1];
148 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
149 uchar_t *dst;
150 const ushort_t *src;
151 size_t inlen, outlen;
152 int flags;
153
154 if (ctx->f_nmlen == 0)
155 return;
156
157 if (!SMB_UNICODE_STRINGS(vcp))
158 return;
159
160 if (ctx->f_namesz < sizeof (tmpbuf)) {
161 ASSERT(0);
162 goto errout;
163 }
164
165 /*
166 * In-place conversions are not supported,
167 * so convert into tmpbuf and copy.
168 */
169 dst = tmpbuf;
170 outlen = SMB_MAXFNAMELEN;
171 /*LINTED*/
172 src = (const ushort_t *)ctx->f_name;
173 inlen = ctx->f_nmlen / 2; /* number of UCS-2 characters */
174 flags = UCONV_IN_LITTLE_ENDIAN;
175
176 if (uconv_u16tou8(src, &inlen, dst, &outlen, flags) != 0)
177 goto errout;
178
179 ASSERT(outlen < sizeof (tmpbuf));
180 tmpbuf[outlen] = '\0';
181 bcopy(tmpbuf, ctx->f_name, outlen + 1);
182 ctx->f_nmlen = (int)outlen;
183 return;
184
185 errout:
186 /*
187 * Conversion failed, but our caller does not
188 * deal with errors here, so just put a "?".
189 * Don't expect to ever see this.
190 */
191 (void) strlcpy(ctx->f_name, "?", ctx->f_namesz);
192 }
193