xref: /freebsd/sys/netsmb/smb_subr.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2000-2001 Boris Popov
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/endian.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/proc.h>
36 #include <sys/lock.h>
37 #include <sys/sysctl.h>
38 #include <sys/socket.h>
39 #include <sys/signalvar.h>
40 #include <sys/mbuf.h>
41 
42 #include <sys/iconv.h>
43 
44 #include <netsmb/smb.h>
45 #include <netsmb/smb_conn.h>
46 #include <netsmb/smb_rq.h>
47 #include <netsmb/smb_subr.h>
48 
49 static MALLOC_DEFINE(M_SMBDATA, "SMBDATA", "Misc netsmb data");
50 static MALLOC_DEFINE(M_SMBSTR, "SMBSTR", "netsmb string data");
51 MALLOC_DEFINE(M_SMBTEMP, "SMBTEMP", "Temp netsmb data");
52 
53 smb_unichar smb_unieol = 0;
54 
55 void
56 smb_makescred(struct smb_cred *scred, struct thread *td, struct ucred *cred)
57 {
58 	if (td) {
59 		scred->scr_td = td;
60 		scred->scr_cred = cred ? cred : td->td_ucred;
61 	} else {
62 		scred->scr_td = NULL;
63 		scred->scr_cred = cred ? cred : NULL;
64 	}
65 }
66 
67 int
68 smb_td_intr(struct thread *td)
69 {
70 	struct proc *p;
71 	sigset_t tmpset;
72 
73 	if (td == NULL)
74 		return 0;
75 
76 	p = td->td_proc;
77 	PROC_LOCK(p);
78 	tmpset = p->p_siglist;
79 	SIGSETOR(tmpset, td->td_siglist);
80 	SIGSETNAND(tmpset, td->td_sigmask);
81 	mtx_lock(&p->p_sigacts->ps_mtx);
82 	SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
83 	mtx_unlock(&p->p_sigacts->ps_mtx);
84 	if (SIGNOTEMPTY(td->td_siglist) && SMB_SIGMASK(tmpset)) {
85 		PROC_UNLOCK(p);
86                 return EINTR;
87 	}
88 	PROC_UNLOCK(p);
89 	return 0;
90 }
91 
92 char *
93 smb_strdup(const char *s)
94 {
95 	char *p;
96 	size_t len;
97 
98 	len = s ? strlen(s) + 1 : 1;
99 	p = malloc(len, M_SMBSTR, M_WAITOK);
100 	if (s)
101 		bcopy(s, p, len);
102 	else
103 		*p = 0;
104 	return p;
105 }
106 
107 /*
108  * duplicate string from a user space.
109  */
110 char *
111 smb_strdupin(char *s, size_t maxlen)
112 {
113 	char *p;
114 	int error;
115 
116 	p = malloc(maxlen + 1, M_SMBSTR, M_WAITOK);
117 	error = copyinstr(s, p, maxlen + 1, NULL);
118 	if (error) {
119 		free(p, M_SMBSTR);
120 		return (NULL);
121 	}
122 	return p;
123 }
124 
125 /*
126  * duplicate memory block from a user space.
127  */
128 void *
129 smb_memdupin(void *umem, size_t len)
130 {
131 	char *p;
132 
133 	if (len > 8 * 1024)
134 		return NULL;
135 	p = malloc(len, M_SMBSTR, M_WAITOK);
136 	if (copyin(umem, p, len) == 0)
137 		return p;
138 	free(p, M_SMBSTR);
139 	return NULL;
140 }
141 
142 /*
143  * duplicate memory block in the kernel space.
144  */
145 void *
146 smb_memdup(const void *umem, int len)
147 {
148 	char *p;
149 
150 	if (len > 8 * 1024)
151 		return NULL;
152 	p = malloc(len, M_SMBSTR, M_WAITOK);
153 	if (p == NULL)
154 		return NULL;
155 	bcopy(umem, p, len);
156 	return p;
157 }
158 
159 void
160 smb_strfree(char *s)
161 {
162 	free(s, M_SMBSTR);
163 }
164 
165 void
166 smb_memfree(void *s)
167 {
168 	free(s, M_SMBSTR);
169 }
170 
171 void *
172 smb_zmalloc(size_t size, struct malloc_type *type, int flags)
173 {
174 
175 	return malloc(size, type, flags | M_ZERO);
176 }
177 
178 void
179 smb_strtouni(u_int16_t *dst, const char *src)
180 {
181 	while (*src) {
182 		*dst++ = htole16(*src++);
183 	}
184 	*dst = 0;
185 }
186 
187 #ifdef SMB_SOCKETDATA_DEBUG
188 void
189 m_dumpm(struct mbuf *m) {
190 	char *p;
191 	size_t len;
192 	printf("d=");
193 	while(m) {
194 		p=mtod(m,char *);
195 		len=m->m_len;
196 		printf("(%zu)",len);
197 		while(len--){
198 			printf("%02x ",((int)*(p++)) & 0xff);
199 		}
200 		m=m->m_next;
201 	}
202 	printf("\n");
203 }
204 #endif
205 
206 int
207 smb_maperror(int eclass, int eno)
208 {
209 	if (eclass == 0 && eno == 0)
210 		return 0;
211 	switch (eclass) {
212 	    case ERRDOS:
213 		switch (eno) {
214 		    case ERRbadfunc:
215 		    case ERRbadmcb:
216 		    case ERRbadenv:
217 		    case ERRbadformat:
218 		    case ERRrmuns:
219 			return EINVAL;
220 		    case ERRbadfile:
221 		    case ERRbadpath:
222 		    case ERRremcd:
223 		    case 66:		/* nt returns it when share not available */
224 		    case 67:		/* observed from nt4sp6 when sharename wrong */
225 			return ENOENT;
226 		    case ERRnofids:
227 			return EMFILE;
228 		    case ERRnoaccess:
229 		    case ERRbadshare:
230 			return EACCES;
231 		    case ERRbadfid:
232 			return EBADF;
233 		    case ERRnomem:
234 			return ENOMEM;	/* actually remote no mem... */
235 		    case ERRbadmem:
236 			return EFAULT;
237 		    case ERRbadaccess:
238 			return EACCES;
239 		    case ERRbaddata:
240 			return E2BIG;
241 		    case ERRbaddrive:
242 		    case ERRnotready:	/* nt */
243 			return ENXIO;
244 		    case ERRdiffdevice:
245 			return EXDEV;
246 		    case ERRnofiles:
247 			return 0;	/* eeof ? */
248 			return ETXTBSY;
249 		    case ERRlock:
250 			return EDEADLK;
251 		    case ERRfilexists:
252 			return EEXIST;
253 		    case 123:		/* dunno what is it, but samba maps as noent */
254 			return ENOENT;
255 		    case 145:		/* samba */
256 			return ENOTEMPTY;
257 		    case ERRnotlocked:
258 			return 0;	/* file become unlocked */
259 		    case 183:
260 			return EEXIST;
261 		    case ERRquota:
262 			return EDQUOT;
263 		}
264 		break;
265 	    case ERRSRV:
266 		switch (eno) {
267 		    case ERRerror:
268 			return EINVAL;
269 		    case ERRbadpw:
270 		    case ERRpasswordExpired:
271 			return EAUTH;
272 		    case ERRaccess:
273 			return EACCES;
274 		    case ERRinvnid:
275 			return ENETRESET;
276 		    case ERRinvnetname:
277 			SMBERROR("NetBIOS name is invalid\n");
278 			return EAUTH;
279 		    case 3:		/* reserved and returned */
280 			return EIO;
281 		    case ERRaccountExpired:
282 		    case ERRbadClient:
283 		    case ERRbadLogonTime:
284 			return EPERM;
285 		    case ERRnosupport:
286 			return EBADRPC;
287 		}
288 		break;
289 	    case ERRHRD:
290 		switch (eno) {
291 		    case ERRnowrite:
292 			return EROFS;
293 		    case ERRbadunit:
294 			return ENODEV;
295 		    case ERRnotready:
296 		    case ERRbadcmd:
297 		    case ERRdata:
298 			return EIO;
299 		    case ERRbadreq:
300 			return EBADRPC;
301 		    case ERRbadshare:
302 			return ETXTBSY;
303 		    case ERRlock:
304 			return EDEADLK;
305 		}
306 		break;
307 	}
308 	SMBERROR("Unmapped error %d:%d\n", eclass, eno);
309 	return EBADRPC;
310 }
311 
312 static int
313 smb_copy_iconv(struct mbchain *mbp, c_caddr_t src, caddr_t dst,
314     size_t *srclen, size_t *dstlen)
315 {
316 	int error;
317 	size_t inlen = *srclen, outlen = *dstlen;
318 
319 	error = iconv_conv((struct iconv_drv*)mbp->mb_udata, &src, &inlen,
320 	    &dst, &outlen);
321 	if (inlen != *srclen || outlen != *dstlen) {
322 		*srclen -= inlen;
323 		*dstlen -= outlen;
324 		return 0;
325 	} else
326 		return error;
327 }
328 
329 int
330 smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
331 	size_t size, int caseopt)
332 {
333 	struct iconv_drv *dp = vcp->vc_toserver;
334 
335 	if (size == 0)
336 		return 0;
337 	if (dp == NULL) {
338 		return mb_put_mem(mbp, src, size, MB_MSYSTEM);
339 	}
340 	mbp->mb_copy = smb_copy_iconv;
341 	mbp->mb_udata = dp;
342 	if (SMB_UNICODE_STRINGS(vcp))
343 		mb_put_padbyte(mbp);
344 	return mb_put_mem(mbp, src, size, MB_MCUSTOM);
345 }
346 
347 int
348 smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
349 	int caseopt)
350 {
351 	int error;
352 
353 	error = smb_put_dmem(mbp, vcp, src, strlen(src), caseopt);
354 	if (error)
355 		return error;
356 	if (SMB_UNICODE_STRINGS(vcp))
357 		return mb_put_uint16le(mbp, 0);
358 	return mb_put_uint8(mbp, 0);
359 }
360 
361 int
362 smb_put_asunistring(struct smb_rq *rqp, const char *src)
363 {
364 	struct mbchain *mbp = &rqp->sr_rq;
365 	struct iconv_drv *dp = rqp->sr_vc->vc_toserver;
366 	u_char c;
367 	int error;
368 
369 	while (*src) {
370 		iconv_convmem(dp, &c, src++, 1);
371 		error = mb_put_uint16le(mbp, c);
372 		if (error)
373 			return error;
374 	}
375 	return mb_put_uint16le(mbp, 0);
376 }
377