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: src/sys/fs/smbfs/smbfs_subr.c,v 1.1.2.2 2003/01/17 08:20:26 tjr Exp $ 33 * $DragonFly: src/sys/vfs/smbfs/smbfs_subr.c,v 1.4 2003/08/07 21:54:36 dillon Exp $ 34 */ 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/malloc.h> 39 #include <machine/clock.h> 40 #include <sys/time.h> 41 #include <sys/vnode.h> 42 #include <sys/sysctl.h> 43 #include <sys/iconv.h> 44 45 #include <netproto/smb/smb.h> 46 #include <netproto/smb/smb_conn.h> 47 #include <netproto/smb/smb_subr.h> 48 #include <netproto/smb/smb_rq.h> 49 #include <netproto/smb/smb_dev.h> 50 51 #include "smbfs.h" 52 #include "smbfs_node.h" 53 #include "smbfs_subr.h" 54 55 MALLOC_DEFINE(M_SMBFSDATA, "SMBFS data", "SMBFS private data"); 56 57 /* 58 * Time & date conversion routines taken from msdosfs. Although leap 59 * year calculation is bogus, it's sufficient before 2100 :) 60 */ 61 /* 62 * This is the format of the contents of the deTime field in the direntry 63 * structure. 64 * We don't use bitfields because we don't know how compilers for 65 * arbitrary machines will lay them out. 66 */ 67 #define DT_2SECONDS_MASK 0x1F /* seconds divided by 2 */ 68 #define DT_2SECONDS_SHIFT 0 69 #define DT_MINUTES_MASK 0x7E0 /* minutes */ 70 #define DT_MINUTES_SHIFT 5 71 #define DT_HOURS_MASK 0xF800 /* hours */ 72 #define DT_HOURS_SHIFT 11 73 74 /* 75 * This is the format of the contents of the deDate field in the direntry 76 * structure. 77 */ 78 #define DD_DAY_MASK 0x1F /* day of month */ 79 #define DD_DAY_SHIFT 0 80 #define DD_MONTH_MASK 0x1E0 /* month */ 81 #define DD_MONTH_SHIFT 5 82 #define DD_YEAR_MASK 0xFE00 /* year - 1980 */ 83 #define DD_YEAR_SHIFT 9 84 /* 85 * Total number of days that have passed for each month in a regular year. 86 */ 87 static u_short regyear[] = { 88 31, 59, 90, 120, 151, 181, 89 212, 243, 273, 304, 334, 365 90 }; 91 92 /* 93 * Total number of days that have passed for each month in a leap year. 94 */ 95 static u_short leapyear[] = { 96 31, 60, 91, 121, 152, 182, 97 213, 244, 274, 305, 335, 366 98 }; 99 100 /* 101 * Variables used to remember parts of the last time conversion. Maybe we 102 * can avoid a full conversion. 103 */ 104 static u_long lasttime; 105 static u_long lastday; 106 static u_short lastddate; 107 static u_short lastdtime; 108 109 void 110 smb_time_local2server(struct timespec *tsp, int tzoff, u_long *seconds) 111 { 112 *seconds = tsp->tv_sec - tzoff * 60 /*- tz.tz_minuteswest * 60 - 113 (wall_cmos_clock ? adjkerntz : 0)*/; 114 } 115 116 void 117 smb_time_server2local(u_long seconds, int tzoff, struct timespec *tsp) 118 { 119 tsp->tv_sec = seconds + tzoff * 60; 120 /*+ tz.tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0)*/; 121 } 122 123 /* 124 * Number of seconds between 1970 and 1601 year 125 */ 126 int64_t DIFF1970TO1601 = 11644473600ULL; 127 128 /* 129 * Time from server comes as UTC, so no need to use tz 130 */ 131 void 132 smb_time_NT2local(int64_t nsec, int tzoff, struct timespec *tsp) 133 { 134 smb_time_server2local(nsec / 10000000 - DIFF1970TO1601, 0, tsp); 135 } 136 137 void 138 smb_time_local2NT(struct timespec *tsp, int tzoff, int64_t *nsec) 139 { 140 u_long seconds; 141 142 smb_time_local2server(tsp, 0, &seconds); 143 *nsec = (((int64_t)(seconds) & ~1) + DIFF1970TO1601) * (int64_t)10000000; 144 } 145 146 void 147 smb_time_unix2dos(struct timespec *tsp, int tzoff, u_int16_t *ddp, 148 u_int16_t *dtp, u_int8_t *dhp) 149 { 150 u_long t, days, year, month, inc; 151 u_short *months; 152 153 /* 154 * If the time from the last conversion is the same as now, then 155 * skip the computations and use the saved result. 156 */ 157 smb_time_local2server(tsp, tzoff, &t); 158 t &= ~1; 159 if (lasttime != t) { 160 lasttime = t; 161 lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT) 162 + (((t / 60) % 60) << DT_MINUTES_SHIFT) 163 + (((t / 3600) % 24) << DT_HOURS_SHIFT); 164 165 /* 166 * If the number of days since 1970 is the same as the last 167 * time we did the computation then skip all this leap year 168 * and month stuff. 169 */ 170 days = t / (24 * 60 * 60); 171 if (days != lastday) { 172 lastday = days; 173 for (year = 1970;; year++) { 174 inc = year & 0x03 ? 365 : 366; 175 if (days < inc) 176 break; 177 days -= inc; 178 } 179 months = year & 0x03 ? regyear : leapyear; 180 for (month = 0; days >= months[month]; month++) 181 ; 182 if (month > 0) 183 days -= months[month - 1]; 184 lastddate = ((days + 1) << DD_DAY_SHIFT) 185 + ((month + 1) << DD_MONTH_SHIFT); 186 /* 187 * Remember dos's idea of time is relative to 1980. 188 * unix's is relative to 1970. If somehow we get a 189 * time before 1980 then don't give totally crazy 190 * results. 191 */ 192 if (year > 1980) 193 lastddate += (year - 1980) << DD_YEAR_SHIFT; 194 } 195 } 196 if (dtp) 197 *dtp = lastdtime; 198 if (dhp) 199 *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000; 200 201 *ddp = lastddate; 202 } 203 204 /* 205 * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that 206 * interval there were 8 regular years and 2 leap years. 207 */ 208 #define SECONDSTO1980 (((8 * 365) + (2 * 366)) * (24 * 60 * 60)) 209 210 static u_short lastdosdate; 211 static u_long lastseconds; 212 213 void 214 smb_dos2unixtime(u_int dd, u_int dt, u_int dh, int tzoff, 215 struct timespec *tsp) 216 { 217 u_long seconds; 218 u_long month; 219 u_long year; 220 u_long days; 221 u_short *months; 222 223 if (dd == 0) { 224 tsp->tv_sec = 0; 225 tsp->tv_nsec = 0; 226 return; 227 } 228 seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1) 229 + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60 230 + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600 231 + dh / 100; 232 /* 233 * If the year, month, and day from the last conversion are the 234 * same then use the saved value. 235 */ 236 if (lastdosdate != dd) { 237 lastdosdate = dd; 238 days = 0; 239 year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT; 240 days = year * 365; 241 days += year / 4 + 1; /* add in leap days */ 242 if ((year & 0x03) == 0) 243 days--; /* if year is a leap year */ 244 months = year & 0x03 ? regyear : leapyear; 245 month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT; 246 if (month < 1 || month > 12) { 247 month = 1; 248 } 249 if (month > 1) 250 days += months[month - 2]; 251 days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1; 252 lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980; 253 } 254 smb_time_server2local(seconds + lastseconds, tzoff, tsp); 255 tsp->tv_nsec = (dh % 100) * 10000000; 256 } 257 258 static int 259 smb_fphelp(struct mbchain *mbp, struct smb_vc *vcp, struct smbnode *np, 260 int caseopt) 261 { 262 struct smbmount *smp= np->n_mount; 263 struct smbnode **npp = smp->sm_npstack; 264 int i, error = 0; 265 266 /* simple_lock(&smp->sm_npslock);*/ 267 i = 0; 268 while (np->n_parent) { 269 if (i++ == SMBFS_MAXPATHCOMP) { 270 /* simple_unlock(&smp->sm_npslock);*/ 271 return ENAMETOOLONG; 272 } 273 *npp++ = np; 274 np = VTOSMB(np->n_parent); 275 } 276 /* if (i == 0) 277 return smb_put_dmem(mbp, vcp, "\\", 2, caseopt);*/ 278 while (i--) { 279 np = *--npp; 280 error = mb_put_uint8(mbp, '\\'); 281 if (error) 282 break; 283 error = smb_put_dmem(mbp, vcp, np->n_name, np->n_nmlen, caseopt); 284 if (error) 285 break; 286 } 287 /* simple_unlock(&smp->sm_npslock);*/ 288 return error; 289 } 290 291 int 292 smbfs_fullpath(struct mbchain *mbp, struct smb_vc *vcp, struct smbnode *dnp, 293 const char *name, int nmlen) 294 { 295 int caseopt = SMB_CS_NONE; 296 int error; 297 298 if (SMB_DIALECT(vcp) < SMB_DIALECT_LANMAN1_0) 299 caseopt |= SMB_CS_UPPER; 300 if (dnp != NULL) { 301 error = smb_fphelp(mbp, vcp, dnp, caseopt); 302 if (error) 303 return error; 304 } 305 if (name) { 306 error = mb_put_uint8(mbp, '\\'); 307 if (error) 308 return error; 309 error = smb_put_dmem(mbp, vcp, name, nmlen, caseopt); 310 if (error) 311 return error; 312 } 313 error = mb_put_uint8(mbp, 0); 314 return error; 315 } 316 317 int 318 smbfs_fname_tolocal(struct smb_vc *vcp, char *name, int nmlen, int caseopt) 319 { 320 /* if (caseopt & SMB_CS_UPPER) 321 iconv_convmem(vcp->vc_toupper, name, name, nmlen); 322 else if (caseopt & SMB_CS_LOWER) 323 iconv_convmem(vcp->vc_tolower, name, name, nmlen);*/ 324 if (vcp->vc_tolocal) 325 iconv_convmem(vcp->vc_tolocal, name, name, nmlen); 326 return 0; 327 } 328