xref: /freebsd/sys/fs/smbfs/smbfs_subr.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1d167cf6fSWarner Losh /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d63027b6SPedro F. Giffuni  *
4d122d784SJoel Dahl  * Copyright (c) 2000-2001 Boris Popov
5681a5bbeSBoris Popov  * All rights reserved.
6681a5bbeSBoris Popov  *
7681a5bbeSBoris Popov  * Redistribution and use in source and binary forms, with or without
8681a5bbeSBoris Popov  * modification, are permitted provided that the following conditions
9681a5bbeSBoris Popov  * are met:
10681a5bbeSBoris Popov  * 1. Redistributions of source code must retain the above copyright
11681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer.
12681a5bbeSBoris Popov  * 2. Redistributions in binary form must reproduce the above copyright
13681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer in the
14681a5bbeSBoris Popov  *    documentation and/or other materials provided with the distribution.
15681a5bbeSBoris Popov  *
16681a5bbeSBoris Popov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17681a5bbeSBoris Popov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18681a5bbeSBoris Popov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19681a5bbeSBoris Popov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20681a5bbeSBoris Popov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21681a5bbeSBoris Popov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22681a5bbeSBoris Popov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23681a5bbeSBoris Popov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24681a5bbeSBoris Popov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25681a5bbeSBoris Popov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26681a5bbeSBoris Popov  * SUCH DAMAGE.
27681a5bbeSBoris Popov  */
28681a5bbeSBoris Popov #include <sys/param.h>
29681a5bbeSBoris Popov #include <sys/systm.h>
30681a5bbeSBoris Popov #include <sys/kernel.h>
313c925ad2SPoul-Henning Kamp #include <sys/clock.h>
32681a5bbeSBoris Popov #include <sys/malloc.h>
33681a5bbeSBoris Popov #include <sys/time.h>
34681a5bbeSBoris Popov #include <sys/vnode.h>
35681a5bbeSBoris Popov #include <sys/sysctl.h>
36681a5bbeSBoris Popov #include <sys/iconv.h>
37681a5bbeSBoris Popov 
38681a5bbeSBoris Popov #include <netsmb/smb.h>
39681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
40681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
41681a5bbeSBoris Popov #include <netsmb/smb_rq.h>
42681a5bbeSBoris Popov #include <netsmb/smb_dev.h>
43681a5bbeSBoris Popov 
44681a5bbeSBoris Popov #include <fs/smbfs/smbfs.h>
45681a5bbeSBoris Popov #include <fs/smbfs/smbfs_node.h>
46681a5bbeSBoris Popov #include <fs/smbfs/smbfs_subr.h>
47681a5bbeSBoris Popov 
485bb84bc8SRobert Watson MALLOC_DEFINE(M_SMBFSDATA, "smbfs_data", "SMBFS private data");
49afe09751SDavide Italiano MALLOC_DEFINE(M_SMBFSCRED, "smbfs_cred", "SMBFS cred data");
50681a5bbeSBoris Popov 
51681a5bbeSBoris Popov void
smb_time_local2server(struct timespec * tsp,int tzoff,u_long * seconds)52681a5bbeSBoris Popov smb_time_local2server(struct timespec *tsp, int tzoff, u_long *seconds)
53681a5bbeSBoris Popov {
5491f1c2b3SPoul-Henning Kamp 	*seconds = tsp->tv_sec - tzoff * 60 /*- tz_minuteswest * 60 -
55681a5bbeSBoris Popov 	    (wall_cmos_clock ? adjkerntz : 0)*/;
56681a5bbeSBoris Popov }
57681a5bbeSBoris Popov 
58681a5bbeSBoris Popov void
smb_time_server2local(u_long seconds,int tzoff,struct timespec * tsp)59681a5bbeSBoris Popov smb_time_server2local(u_long seconds, int tzoff, struct timespec *tsp)
60681a5bbeSBoris Popov {
61681a5bbeSBoris Popov 	tsp->tv_sec = seconds + tzoff * 60;
62681a5bbeSBoris Popov }
63681a5bbeSBoris Popov 
64681a5bbeSBoris Popov /*
65681a5bbeSBoris Popov  * Number of seconds between 1970 and 1601 year
66681a5bbeSBoris Popov  */
671493ed41SEric Anholt static int64_t DIFF1970TO1601 = 11644473600ULL;
68681a5bbeSBoris Popov 
69681a5bbeSBoris Popov /*
70681a5bbeSBoris Popov  * Time from server comes as UTC, so no need to use tz
71681a5bbeSBoris Popov  */
72681a5bbeSBoris Popov void
smb_time_NT2local(int64_t nsec,int tzoff,struct timespec * tsp)73681a5bbeSBoris Popov smb_time_NT2local(int64_t nsec, int tzoff, struct timespec *tsp)
74681a5bbeSBoris Popov {
75681a5bbeSBoris Popov 	smb_time_server2local(nsec / 10000000 - DIFF1970TO1601, 0, tsp);
76681a5bbeSBoris Popov }
77681a5bbeSBoris Popov 
78681a5bbeSBoris Popov void
smb_time_local2NT(struct timespec * tsp,int tzoff,int64_t * nsec)79681a5bbeSBoris Popov smb_time_local2NT(struct timespec *tsp, int tzoff, int64_t *nsec)
80681a5bbeSBoris Popov {
81681a5bbeSBoris Popov 	u_long seconds;
82681a5bbeSBoris Popov 
83681a5bbeSBoris Popov 	smb_time_local2server(tsp, 0, &seconds);
84681a5bbeSBoris Popov 	*nsec = (((int64_t)(seconds) & ~1) + DIFF1970TO1601) * (int64_t)10000000;
85681a5bbeSBoris Popov }
86681a5bbeSBoris Popov 
87681a5bbeSBoris Popov void
smb_time_unix2dos(struct timespec * tsp,int tzoff,u_int16_t * ddp,u_int16_t * dtp,u_int8_t * dhp)88681a5bbeSBoris Popov smb_time_unix2dos(struct timespec *tsp, int tzoff, u_int16_t *ddp,
89681a5bbeSBoris Popov 	u_int16_t *dtp,	u_int8_t *dhp)
90681a5bbeSBoris Popov {
913c925ad2SPoul-Henning Kamp 	struct timespec tt;
923c925ad2SPoul-Henning Kamp 	u_long t;
93681a5bbeSBoris Popov 
943c925ad2SPoul-Henning Kamp 	tt = *tsp;
95681a5bbeSBoris Popov 	smb_time_local2server(tsp, tzoff, &t);
963c925ad2SPoul-Henning Kamp 	tt.tv_sec = t;
973c925ad2SPoul-Henning Kamp 	timespec2fattime(&tt, 1, ddp, dtp, dhp);
98681a5bbeSBoris Popov }
99681a5bbeSBoris Popov 
100681a5bbeSBoris Popov void
smb_dos2unixtime(u_int dd,u_int dt,u_int dh,int tzoff,struct timespec * tsp)101681a5bbeSBoris Popov smb_dos2unixtime(u_int dd, u_int dt, u_int dh, int tzoff,
102681a5bbeSBoris Popov 	struct timespec *tsp)
103681a5bbeSBoris Popov {
104681a5bbeSBoris Popov 
1053c925ad2SPoul-Henning Kamp 	fattime2timespec(dd, dt, dh, 1, tsp);
1063c925ad2SPoul-Henning Kamp 	smb_time_server2local(tsp->tv_sec, tzoff, tsp);
107681a5bbeSBoris Popov }
108681a5bbeSBoris Popov 
109681a5bbeSBoris Popov int
smbfs_fullpath(struct mbchain * mbp,struct smb_vc * vcp,struct smbnode * dnp,const char * name,int nmlen)110681a5bbeSBoris Popov smbfs_fullpath(struct mbchain *mbp, struct smb_vc *vcp, struct smbnode *dnp,
111681a5bbeSBoris Popov 	const char *name, int nmlen)
112681a5bbeSBoris Popov {
113681a5bbeSBoris Popov 	int caseopt = SMB_CS_NONE;
114681a5bbeSBoris Popov 	int error;
115681a5bbeSBoris Popov 
11641f1dcccSKevin Lo 	if (SMB_UNICODE_STRINGS(vcp)) {
11741f1dcccSKevin Lo 		error = mb_put_padbyte(mbp);
11841f1dcccSKevin Lo 		if (error)
11941f1dcccSKevin Lo 			return error;
12041f1dcccSKevin Lo 	}
121681a5bbeSBoris Popov 	if (SMB_DIALECT(vcp) < SMB_DIALECT_LANMAN1_0)
122681a5bbeSBoris Popov 		caseopt |= SMB_CS_UPPER;
123681a5bbeSBoris Popov 	if (dnp != NULL) {
12480704a47SDavide Italiano 		error = smb_put_dmem(mbp, vcp, dnp->n_rpath, dnp->n_rplen,
12580704a47SDavide Italiano 		    caseopt);
126681a5bbeSBoris Popov 		if (error)
127681a5bbeSBoris Popov 			return error;
128681a5bbeSBoris Popov 		if (name) {
12980704a47SDavide Italiano 			/* Put the separator */
13041f1dcccSKevin Lo 			if (SMB_UNICODE_STRINGS(vcp))
13141f1dcccSKevin Lo 				error = mb_put_uint16le(mbp, '\\');
13241f1dcccSKevin Lo 			else
133681a5bbeSBoris Popov 				error = mb_put_uint8(mbp, '\\');
134681a5bbeSBoris Popov 			if (error)
135681a5bbeSBoris Popov 				return error;
13680704a47SDavide Italiano 			/* Put the name */
137681a5bbeSBoris Popov 			error = smb_put_dmem(mbp, vcp, name, nmlen, caseopt);
138681a5bbeSBoris Popov 			if (error)
139681a5bbeSBoris Popov 				return error;
140681a5bbeSBoris Popov 		}
14180704a47SDavide Italiano 	}
14280704a47SDavide Italiano 	/* Put NULL terminator. */
14380704a47SDavide Italiano 	if (SMB_UNICODE_STRINGS(vcp))
14480704a47SDavide Italiano 		error = mb_put_uint16le(mbp, 0);
14580704a47SDavide Italiano 	else
14641f1dcccSKevin Lo 		error = mb_put_uint8(mbp, 0);
147681a5bbeSBoris Popov 	return error;
148681a5bbeSBoris Popov }
149681a5bbeSBoris Popov 
150681a5bbeSBoris Popov int
smbfs_fname_tolocal(struct smb_vc * vcp,char * name,int * nmlen,int caseopt)1514ebd3ea1STakanori Watanabe smbfs_fname_tolocal(struct smb_vc *vcp, char *name, int *nmlen, int caseopt)
152681a5bbeSBoris Popov {
1534ebd3ea1STakanori Watanabe 	int copt = (caseopt == SMB_CS_LOWER ? KICONV_FROM_LOWER :
1544ebd3ea1STakanori Watanabe 		    (caseopt == SMB_CS_UPPER ? KICONV_FROM_UPPER : 0));
1554ebd3ea1STakanori Watanabe 	int error = 0;
1561e8a6960STakanori Watanabe 	size_t ilen = *nmlen;
1571e8a6960STakanori Watanabe 	size_t olen;
1584ebd3ea1STakanori Watanabe 	char *ibuf = name;
1594ebd3ea1STakanori Watanabe 	char outbuf[SMB_MAXFNAMELEN];
1604ebd3ea1STakanori Watanabe 	char *obuf = outbuf;
1614ebd3ea1STakanori Watanabe 
1624ebd3ea1STakanori Watanabe 	if (vcp->vc_tolocal) {
1634ebd3ea1STakanori Watanabe 		olen = sizeof(outbuf);
1644ebd3ea1STakanori Watanabe 		bzero(outbuf, sizeof(outbuf));
1654ebd3ea1STakanori Watanabe 
1664ebd3ea1STakanori Watanabe 		/*
1674ebd3ea1STakanori Watanabe 		error = iconv_conv_case
1684ebd3ea1STakanori Watanabe 			(vcp->vc_tolocal, NULL, NULL, &obuf, &olen, copt);
1694ebd3ea1STakanori Watanabe 		if (error) return error;
1704ebd3ea1STakanori Watanabe 		*/
1714ebd3ea1STakanori Watanabe 
1727f4daa88SDimitry Andric 		error = iconv_conv_case(vcp->vc_tolocal,
1737f4daa88SDimitry Andric 		    __DECONST(const char **, &ibuf), &ilen, &obuf, &olen, copt);
17441f1dcccSKevin Lo 		if (error && SMB_UNICODE_STRINGS(vcp)) {
17541f1dcccSKevin Lo 			/*
17641f1dcccSKevin Lo 			 * If using unicode, leaving a file name as it was when
17741f1dcccSKevin Lo 			 * convert fails will cause a problem because the file name
17841f1dcccSKevin Lo 			 * will contain NULL.
17941f1dcccSKevin Lo 			 * Here, put '?' and give converted file name.
18041f1dcccSKevin Lo 			 */
18141f1dcccSKevin Lo 			*obuf = '?';
18241f1dcccSKevin Lo 			olen--;
18341f1dcccSKevin Lo 			error = 0;
18441f1dcccSKevin Lo 		}
1854ebd3ea1STakanori Watanabe 		if (!error) {
1864ebd3ea1STakanori Watanabe 			*nmlen = sizeof(outbuf) - olen;
1874ebd3ea1STakanori Watanabe 			memcpy(name, outbuf, *nmlen);
1884ebd3ea1STakanori Watanabe 		}
1894ebd3ea1STakanori Watanabe 	}
1904ebd3ea1STakanori Watanabe 	return error;
191681a5bbeSBoris Popov }
192afe09751SDavide Italiano 
193afe09751SDavide Italiano void *
smbfs_malloc_scred(void)194afe09751SDavide Italiano smbfs_malloc_scred(void)
195afe09751SDavide Italiano {
196afe09751SDavide Italiano 	return (malloc(sizeof(struct smb_cred), M_SMBFSCRED, M_WAITOK));
197afe09751SDavide Italiano }
198afe09751SDavide Italiano 
199afe09751SDavide Italiano void
smbfs_free_scred(void * scred)200afe09751SDavide Italiano smbfs_free_scred(void *scred)
201afe09751SDavide Italiano {
202afe09751SDavide Italiano 	free(scred, M_SMBFSCRED);
203afe09751SDavide Italiano }
204