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 * $FreeBSD: releng/8.4/sys/fs/smbfs/smbfs_subr.c 230205 2012-01-16 08:29:52Z kevlo $
33 */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/clock.h>
38 #include <sys/malloc.h>
39 #include <sys/time.h>
40 #include <sys/vnode.h>
41 #include <sys/sysctl.h>
42 #include <sys/iconv.h>
43
44 #include <netsmb/smb.h>
45 #include <netsmb/smb_conn.h>
46 #include <netsmb/smb_subr.h>
47 #include <netsmb/smb_rq.h>
48 #include <netsmb/smb_dev.h>
49
50 #include <fs/smbfs/smbfs.h>
51 #include <fs/smbfs/smbfs_node.h>
52 #include <fs/smbfs/smbfs_subr.h>
53
54 MALLOC_DEFINE(M_SMBFSDATA, "smbfs_data", "SMBFS private data");
55
56 void
57 smb_time_local2server(struct timespec *tsp, int tzoff, u_long *seconds)
58 {
59 *seconds = tsp->tv_sec - tzoff * 60 /*- tz_minuteswest * 60 -
60 (wall_cmos_clock ? adjkerntz : 0)*/;
61 }
62
63 void
64 smb_time_server2local(u_long seconds, int tzoff, struct timespec *tsp)
65 {
66 tsp->tv_sec = seconds + tzoff * 60;
67 /*+ tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0)*/;
68 }
69
70 /*
71 * Number of seconds between 1970 and 1601 year
72 */
73 static int64_t DIFF1970TO1601 = 11644473600ULL;
74
75 /*
76 * Time from server comes as UTC, so no need to use tz
77 */
78 void
79 smb_time_NT2local(int64_t nsec, int tzoff, struct timespec *tsp)
80 {
81 smb_time_server2local(nsec / 10000000 - DIFF1970TO1601, 0, tsp);
82 }
83
84 void
85 smb_time_local2NT(struct timespec *tsp, int tzoff, int64_t *nsec)
86 {
87 u_long seconds;
88
89 smb_time_local2server(tsp, 0, &seconds);
90 *nsec = (((int64_t)(seconds) & ~1) + DIFF1970TO1601) * (int64_t)10000000;
91 }
92
93 void
94 smb_time_unix2dos(struct timespec *tsp, int tzoff, u_int16_t *ddp,
95 u_int16_t *dtp, u_int8_t *dhp)
96 {
97 struct timespec tt;
98 u_long t;
99
100 tt = *tsp;
101 smb_time_local2server(tsp, tzoff, &t);
102 tt.tv_sec = t;
103 timespec2fattime(&tt, 1, ddp, dtp, dhp);
104 }
105
106 void
107 smb_dos2unixtime(u_int dd, u_int dt, u_int dh, int tzoff,
108 struct timespec *tsp)
109 {
110
111 fattime2timespec(dd, dt, dh, 1, tsp);
112 smb_time_server2local(tsp->tv_sec, tzoff, tsp);
113 }
114
115 static int
116 smb_fphelp(struct mbchain *mbp, struct smb_vc *vcp, struct smbnode *np,
117 int caseopt)
118 {
119 struct smbmount *smp= np->n_mount;
120 struct smbnode **npp = smp->sm_npstack;
121 int i, error = 0;
122
123 /* simple_lock(&smp->sm_npslock);*/
124 i = 0;
125 while (np->n_parent) {
126 if (i++ == SMBFS_MAXPATHCOMP) {
127 /* simple_unlock(&smp->sm_npslock);*/
128 return ENAMETOOLONG;
129 }
130 *npp++ = np;
131 if ((np->n_flag & NREFPARENT) == 0)
132 break;
133 np = VTOSMB(np->n_parent);
134 }
135 /* if (i == 0)
136 return smb_put_dmem(mbp, vcp, "\\", 2, caseopt);*/
137 while (i--) {
138 np = *--npp;
139 if (SMB_UNICODE_STRINGS(vcp))
140 error = mb_put_uint16le(mbp, '\\');
141 else
142 error = mb_put_uint8(mbp, '\\');
143 if (error)
144 break;
145 error = smb_put_dmem(mbp, vcp, np->n_name, np->n_nmlen, caseopt);
146 if (error)
147 break;
148 }
149 /* simple_unlock(&smp->sm_npslock);*/
150 return error;
151 }
152
153 int
154 smbfs_fullpath(struct mbchain *mbp, struct smb_vc *vcp, struct smbnode *dnp,
155 const char *name, int nmlen)
156 {
157 int caseopt = SMB_CS_NONE;
158 int error;
159
160 if (SMB_UNICODE_STRINGS(vcp)) {
161 error = mb_put_padbyte(mbp);
162 if (error)
163 return error;
164 }
165 if (SMB_DIALECT(vcp) < SMB_DIALECT_LANMAN1_0)
166 caseopt |= SMB_CS_UPPER;
167 if (dnp != NULL) {
168 error = smb_fphelp(mbp, vcp, dnp, caseopt);
169 if (error)
170 return error;
171 }
172 if (name) {
173 if (SMB_UNICODE_STRINGS(vcp))
174 error = mb_put_uint16le(mbp, '\\');
175 else
176 error = mb_put_uint8(mbp, '\\');
177 if (error)
178 return error;
179 error = smb_put_dmem(mbp, vcp, name, nmlen, caseopt);
180 if (error)
181 return error;
182 }
183 error = mb_put_uint8(mbp, 0);
184 if (SMB_UNICODE_STRINGS(vcp) && error == 0)
185 error = mb_put_uint8(mbp, 0);
186 return error;
187 }
188
189 int
190 smbfs_fname_tolocal(struct smb_vc *vcp, char *name, int *nmlen, int caseopt)
191 {
192 int copt = (caseopt == SMB_CS_LOWER ? KICONV_FROM_LOWER :
193 (caseopt == SMB_CS_UPPER ? KICONV_FROM_UPPER : 0));
194 int error = 0;
195 size_t ilen = *nmlen;
196 size_t olen;
197 char *ibuf = name;
198 char outbuf[SMB_MAXFNAMELEN];
199 char *obuf = outbuf;
200
201 if (vcp->vc_tolocal) {
202 olen = sizeof(outbuf);
203 bzero(outbuf, sizeof(outbuf));
204
205 /*
206 error = iconv_conv_case
207 (vcp->vc_tolocal, NULL, NULL, &obuf, &olen, copt);
208 if (error) return error;
209 */
210
211 error = iconv_conv_case
212 (vcp->vc_tolocal, (const char **)&ibuf, &ilen, &obuf, &olen, copt);
213 if (error && SMB_UNICODE_STRINGS(vcp)) {
214 /*
215 * If using unicode, leaving a file name as it was when
216 * convert fails will cause a problem because the file name
217 * will contain NULL.
218 * Here, put '?' and give converted file name.
219 */
220 *obuf = '?';
221 olen--;
222 error = 0;
223 }
224 if (!error) {
225 *nmlen = sizeof(outbuf) - olen;
226 memcpy(name, outbuf, *nmlen);
227 }
228 }
229 return error;
230 }
Cache object: 9e77e0a7158aaa1e4f88ba72da5be9ee
|